310 likes | 473 Views
CSE 30331 Lectures 19 – Intro to Graphs. Searching (DFS & BFS) Connected Components Graph G and Its Transpose G T Shortest-Path Example Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree. Graph traversals & searches. Breadth first search (BFS) Searches in expanding rings
E N D
CSE 30331Lectures 19 – Intro to Graphs • Searching (DFS & BFS) • Connected Components • Graph G and Its Transpose GT • Shortest-Path Example • Dijkstra’s Minimum-Path Algorithm • Minimum Spanning Tree
Graph traversals & searches • Breadth first search (BFS) • Searches in expanding rings • All vertices one edge away, then all vertices two edges away, etc • Depth first search (DFS) • Searches as deeply as possible until trapped, then backtracks and searches again • Search backtracks when • No more edges exit current vertex • No more of unused out edges from current node go to vertices that have not been already discovered
Breadth First Search (traversal) • Uses a queue to order search and a set to store visited vertices • Start with all unvisited (white) vertices • Push start vertex onto Q • While Q is not empty • Remove vertex V from Q • Mark V as visited (black) • Insert V into the visited set • For each adjacent vertex (each neighbor) U of V • If U is unvisited (white) • Mark it seen (gray) and push it onto Q • Return visited set (vertices reached from start) • Running time: O(V + E)
How many Edges are there? • Given a graph with V vertices • A complete undirected graph has an edge between every pair of vertices • So E = (V-1) + (V-2) + ... + 2 + 1 = V(V-1)/2 • E is O(V2) • An unconnected graph has no edges • A complete digraph has two edges between every pair of vertices • So E = V(V-1) and E is still O(V2) • If loops are allowed (nodes may have themselves as neighbors) • E = V2, still O(V2)
Quick Aside • A back edge is one linking the current vertex with another that • Has already been seen or • Has already been visited • Back edges indicate potential cycles and/or connected components
Depth first search (traversal of entire graph) • Emulates a postorder traversal, backtracking search • Visits occur while backing out • Dfs(G, dfsList) • Clear(dfsList) • For every vertex V in G • Mark V as unvisited (white) • For every vertex V in G • If V is unvisited (white) • dfsVisit(G, V, false)
Depth first search Visit (from vertex V) • Emulates a postorder traversal, backtracking search • Visits occur while backing out • DfsVisit(V, checkCycles) • If V is unvisited (white) • Mark V as seen (gray) • For each neighbor U of V • If U is unvisited (white) • DfsVisit(U,checkCycles) • Else if U is previously discovered (gray) && checkCycles • Throw exception (found cycle) • Mark V as visited (black) • Push V onto FRONT of dfsList
1/7 A 2/3 5/6 B E 3/2 6/4 7/5 Back edge C F G 4/1 m/n == discovered/visited D dfs() discovery and visiting
Strong Components • A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.
Graph G and Its Transpose GT • The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.
Finding Strong Components • Perform dfs() of graph G, creating dfsGList • Create GT (transform of graph G) • Color all vertices in GT white • For each vertex V in GT taken in order of dfsGList • If V is white • Perform dfsVisit() of GT from V and create dfsGTList • Append dfsGTList to component vector • At end of process, the component vector contains a set of vertices for each strong component in the graph G • Finding GT is O(V+E) and dfs() is O(V+E) • So, finding strong components is also O(V+E)
G, GT and its Strong Components • dfsGList: A B C E D G F • dfsGTLists: {A C B} , {E} , {D F G}
Topological sort of acyclic graphs • Important in determining precedence order in graphs representing scheduling of activities • Dfs() produces a topological sort of the vertices in the graph, returning them in the dfsList • Graph must be acyclic • Example: • Given a graph of courses (with prerequisites) for a major • Dfs() gives a topological sort indicating a possible 4 year sequence of classes • To show that dfs() performs a topological sort • show that if a path exists from V to W then V always appears ahead of W in dfsList • We examine the three colors W may have when first encountered in path …
Shortest-Path Example • Shortest-path is a modified breadth-first search • Path length is number of edges traversed and is stored in dataValue field of vertex at time of its discovery • The parent field is set to the index of the parent at the same time • Path is recovered in reverse, using parent fields
Shortest-Path Example(path from F to C) • Start: visitQ = F:0:F format(vertex:dataValue:parent) • Next: visitQ = D:1:F, E:1:F • Next: visitQ = E:1:F, A:2:D • Next: visitQ = A:2:D • Next: visitQ = B:3:A, C:3:A • Next: visitQ = C:3:A • Finish: C found, path length = 3, path = F,D,A,C : parent( parent( parent(C) ) )
Minimum (weight) path – Dijkstra’s algorithm • Uses priority queue containing identities of all fringe vertices and the length of the minimum path to each from the start • Algorithm builds a tree of all minimum length paths from start • Each vertex is either tree, fringe or unseen At each step The fringe vertex V with the minimum path is removed from priorityQ and added to the tree V’s non-tree neighbors U become fringe and the minimum path length is computed from start, thru V to U and is stored in U.dataValue, V is saved as U.parent and U is added to priorityQ • Process stops when queue is empty, or chosen destination vertex is found
Dijkstra Minimum-Path Algorithm (Example A to D) PriQ: (A,0) Tree (vertices & path weight) (B,4) (C,11) (E,4) A,0 (E,4) (C,11) (C,10) (D,12) A,0 B,4 (C,10) (C,11) (D,12) A,0 B,4 E,4 (C,11) (D,12) A,0 B,4 E,4 C,10 (D,12) A,0 B,4 E,4 C,10 empty A,0 B,4 E,4 C,10 D,12
Minimum Spanning TreePrim’s Algorithm • Spanning tree for graph with minimum TOTAL weight • Minimum Spanning Tree may not be unique, but total weight is same value for all • All vertices are either tree, fringe, or unseen • Priority queue is used to hold fringe vertices and the minimum weight edge connecting each to the tree Put start vertex in priorityQ While priorityQ not empty The nearest vertex V is removed from the queue and added to the tree For each non-tree neighbor U of V if the edge V,U weight < current U.dataValue U.dataValue is set to weight of edge V,U U.parent is set to V push U:weight pair onto priority queue
A A 2 12 2 B C 5 B 7 8 Spanning tree with vertices A, B D minSpanTreeSize = 2, minTreeWeight = 2 Minimum Spanning Tree: Step 1 (edge A-B)
A A 2 2 12 5 B B C 5 7 8 D D Spanning tree with vertices A, B, D minSpanTreeSize = 3, minTreeWeight = 7 Minimum Spanning Tree:Step 2 (Edge A-D)
A A 2 2 12 5 B B C 5 C 7 7 8 D D Spanning tree with vertices A, B, D, C minSpanTreeSize = 4, minTreeWeight = 14 Minimum Spanning Tree:Step 3 (Edge D-C)
Runtime Orders of Complexity • Min Spanning Tree – O(V + E log2E) • Min Path (Dijkstra) – O(V + E log2E) • Strong Components – O(V + E) • Dfs – O(V+E) • BFS – O(V+E)
Graphs – Important Terms • Vertex, edge, adjacency, path, cycle • Directed (digraph), undirected • Complete • Connected (strongly, weakly, components) • Searches (DFS, BFS) • Shortest Path, Minimum Path • Euler Path, Hamiltonian Path • Minimum Spanning Tree
Searching Graphs • Breadth-First Search, bfs() • Locates all vertices reachable from a starting vertex • Uses a queue in process • Can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.
Searching Graphs • Depth-First Search, dfs() • Produces a list of all graph vertices in the reverse order of their finishing times. • Supported by a recursive depth-first visit function, dfsVisit() • An algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG) • Forms the basis for an efficient algorithm that finds the strong components of a graph
Searching Graphs • Dijkstra's algorithm (minimum path) • Uses a priority queue to determine a path from a starting to an ending vertex, of minimum weight • Prim's algorithm (minimum spanning tree) • An extension of Dijkstra’s algorithm, which computes the minimum spanning tree of an undirected, connected graph.