290 likes | 413 Views
Midterm Monday Oct . 20. Chapter 3 Decompositions of Graphs. Graphs. Graph Depth-first search in undirected graphs Depth-first search in directed graphs Forward edge Cross edge Back edge Strongly connected components Union-Find. Graph. Graph G = ( V , E ) V = set of vertices
E N D
Graphs • Graph • Depth-first search in undirected graphs • Depth-first search in directed graphs • Forward edge • Cross edge • Back edge • Strongly connected components • Union-Find
Graph • Graph G = (V, E) • V = set of vertices • E = set of edges (VV)
a b b d c a 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b a c b c d a b c d c d d a c 3 4 Representation of Graphs • Two standard ways. • Adjacency Lists. • Adjacency Matrix.
a b b d c a c b c d c d d a b b d c a a c b c d a b c d d a c Adjacency Lists • Consists of an array Adj of |V| lists. • One list per vertex. • For uV, Adj[u] consists of all vertices adjacent to u. If weighted, store weights also in adjacency lists.
1 2 1 2 3 4 1 0 1 1 1 2 0 0 1 0 3 0 0 0 1 4 0 0 0 0 a b c d 4 3 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b c d 3 4 Adjacency Matrix • |V| |V| matrix A. • Number vertices from 1 to |V| in some arbitrary manner. • A is then given by: A = AT for undirected graphs.
Adjacency Matrix vs Adjacency List • Adjacency matrix uses fixed amount of space • Depends on number of vertices • Does not depend on number of edges • Presence of an edge between two vertices can be known immediately • All neighbors of a vertex found by scanning entire row for that vertex • Adjacency list represents only edges that originate from the vertex • Space not reserved for edges that do not exist • more often used than adjacency matrix
DFS Depth-first search in undirected graphs
DFS • Implementation • Stack • Cost? • O(|V|+|E|)
DFS Depth-first search in directed graphs
DAG Directed acyclic graphs
Strongly connected components Directed graph: u and v are connectediff there is a path from u to v and a path from v to u. 5 strongly connected components
Strongly connected components • Undirected graph • DFS
Union/Find Disjoint Sets Union/Find Algorithms
Implementation (cont.) The parent pointer representation is often used to maintain a collection of disjoint sets and support the two basic operations: • Check if two objects are in same set • Merge two sets
I A B D F J C H E G Example: A Network Graph
Find Root • Check if two elements are in same tree // Return TRUE if nodes in different trees boolean differ(int a, int b) { int root1 = find(a); // Find root for a int root2 = find(b); // Find root for b return root1 != root2; // Compare roots }
F A G D B C I E H Merge Two Sets UNION/FIND
Equiv Class Processing (1) (A,B) (C,H) (G,F) (D,E) (I,F) (H,A) (E,G)
Union/Find int find(int curr) { while (array[curr]!=ROOT) curr = array[curr]; return curr; // At root } void union(int a, int b) { int root1 = find(a); // Find root for a int root2 = find(b); // Find root for b if (root1 != root2) array[root2] = root1; } Want to keep the depth small. Weighted union rule: Join the tree with fewer nodes to the tree with more nodes.
Path Compression • Find: • Return root of current node • Reset every node on the path from curr to the • root to point directly to the root.
G D D G A A H B C E E F F I I H B C Path Compression (cont.) Find: • Return root of current node • Reset every node on the path from curr to the root to point directly to the root.
D G A E F I H B C Path Compression (cont.) UNION: