Lecture Notes on Graphs Graphs * A graph is a network of vertices and edges. * In 1736 Leonhard Euler presented the solution to the Bridges of Konigsberg (now Kaliningard) problem - this was the beginning of Graph Theory https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Konigsberg_Bridges.png * Types of Graphs - (un) weighted, (un) directed - (dis) connected - (in) complete - (a) cyclic - bipartite * Degree of a vertex - number of edges that are incident on the vertex * Eulerian cycle (circuit) - visit every edge just once and return to the starting vertex. * Hamiltonian cycle (circuit) - visit every vertex exactly once and return to the starting vertex. * The Eulerian Path or Hamiltonian Path differs from the cycle in that the starting and ending vertices are different. * Graph Representation - adjacency matrix - adjacency lists (linked lists) https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Representation.pdf * Graph Traversals - depth first search (dfs) - breadth first search (bfs) * Depth First Search (DFS) 0. Create a Stack. 1. Select a starting vertex. Make it the current vertex. Mark it visited. Push it on the stack. 2. If possible, visit an adjacent unvisited vertex from the current vertex in order. Make it the current vertez. Mark it visited, and push it on the stack. 3. If you cannot follow step 2, then if possible pop a vertex from the stack. Make it the current vertex. 4. Repeat steps 2 and 3 until the stack is empty. * Breadth First Search (BFS) 0. Create a Queue. 1. Select a starting vertex. Make it the current vertex. Mark it visited. 2. Visit an adjacent unvisited vertex (if there is one) in order from the current vertex. Mark it visited and insert it into the queue. 3. If you cannot carry out step 2 because there are no more unvisited vertices, remove a vertex from the queue (if possible) and make it the current vertex. 4. Repeat steps 2 and 3 until the queue is empty. * Definitions - bipartite and isomorphism https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Terms.pdf https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Images.pdf * Directed Acyclic Graphs (DAGs) https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/DAG.pdf Topological Sort (Topo Sort) Works on directed graphs that do not have cycles (DAGs) 0. Determine the in_degree for all vertices. The in_degree is the number of edges that are incident on that vertex. 1. Remove the vertices that have an in_degree of 0 to a list and remove the out going edges from those vertices. Sort the list in a given order. Enqueue the vertices into a Queue and then update the in_degree of all remaining vertices. 2. Repeat step 1 until there are no more vertices in the Graph. 3. Dequeue the vertices and print. * Exercise do a topo sort on the following DAG and print the vertices at a given level in alphabetical order https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/dag.gif * Minimum Spanning Tree (MST) - Kruskal's Algorithm and Prim's Algorithm. * Kruskal's Algorithm - Order the edges by increasing weight. - Start with an empty graph having only the vertices but no edges. Add an edge to this graph as long as it does not form a cycle. - When all the vertices are connected you are done. * Prim's Algorithm - Start with an empty graph having only the vertices. - Start with any vertex and add it to the list of visited vertices. - Choose the edge with the smallest weight to an unvisited vertex as long as it does not form a cycle. Add that vertex to the list of visited vertices. - Keep adding the edges with the smallest weight from any of the vertices in the list of visited vertices as long as those edges do not form a cycle. - When all the vertices have been visited then you are done. * Do Exercises on Graphs https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Exercises.pdf https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Minimum_Spanning_Tree.pdf * Dijkstra's Single Source Shortest Path Algorithm - Choose a starting vertex. - Create a table with all the other vertices in the graph. - From the starting vertex fill the table with the cost of visiting all the other vertices. - Go to the vertex with the lowest cost and update the cost of visiting all the other vertices from the starting vertex. - Go to all the other vertices in order of increasing cost and update the costs of visiting the other vertices from the starting vertex. - When you have visited all the vertices that you can visit from the starting vertex then you are done. * Worked out example using Dijkstra's Algorithm https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Dijkstra.pdf * Let us look at some fun puzzles that you can use graphs to solve: https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Puzzles.pdf * Here is one solution: https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Images.pdf * Some additional terms: https://www.cs.utexas.edu/users/mitra/csFall2024/cs329/notes/Graph_Terms.pdf