Bipartiteness
Graph G = (V,E) is bipartite iff it can be partitioned into two sets of nodes A and B such that each edge has one end in A and the other Introduction to Algorithms end in B Alternatively: Graph Algorithms • Graph G = (V,E) is bipartite iff all its cycles have even length • Graph G = (V,E) is bipartite iff nodes can be coloured using two colours CSE 680 Question: given a graph G, how to test if the graph is bipartite? Prof. Roger Crawfis Note: graphs without cycles (trees) are bipartite
bipartite:
non bipartite
Testing bipartiteness Toppgological Sort Method: use BFS search tree Recall: BFS is a rooted spanning tree. Algorithm: Want to “sort” or linearize a directed acyygp()clic graph (DAG). • Run BFS search and colour all nodes in odd layers red, others blue • Go through all edges in adjacency list and check if each of them has two different colours at its ends - if so then G is bipartite, otherwise it is not A B D We use the following alternative definitions in the analysis: • Graph G = (V,E) is bipartite iff all its cycles have even length, or • Graph G = (V, E) is bipartite iff it has no odd cycle C E
A B C D E
bipartit e non bipartite Toppgological Sort Example
z PerformedonaPerformed on a DAG. A B D z Linear ordering of the vertices of G such that if 1/ (u, v) ∈ E, then u appears before v.
Topological-Sort (G) 1. call DFS(G) to compute finishing times f [v] for all v ∈ V 2. as each vertex is finished, insert it onto the front of a linked list C E 3. return the linked list of vertices
Linked List: Time: Θ(V + E).
Example Example
A B D A B D 1/ 1/
2/ 2/3 C E C E
Linked List: Linked List:
2/3 E Example Example
A B D A B D 1/4 5/ 1/4
2/3 2/3 C E C E
Linked List: Linked List:
1/4 2/3 1/4 2/3 D E D E
Example Example
A B D A B D 5/ 1/4 5/ 1/4
6/ 2/3 6/7 2/3 C E C E
Linked List: Linked List:
1/4 2/3 6/7 1/4 2/3 D E C D E Example Example
A B D A B D 5/8 1/4 9/ 5/8 1/4
6/7 2/3 6/7 2/3 C E C E
Linked List: Linked List:
5/8 6/7 1/4 2/3 5/8 6/7 1/4 2/3 B C D E B C D E
Example Precedence Example
z Tasks that have to be done to eat A B D breakfast: 9/10 5/8 1/4 z get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat. 6/7 2/3 C E z Certain events must happen in a certain order (ex: get bowl before pouring milk) Linked List: z Fthtitd'ttt(For other events, it doesn't matter (ex: 9/10 5/8 6/7 1/4 2/3 get bowl and get spoon) A B C D E Precedence Example Precedence Example
z Topological Sort get glass get bowl
pour juice 1 2 3 4 5 6 7 8 9 10 11 12 13 14 pour cereal
eat milk get spoon spoon pour milk juice cereal
glass bowl eat breakfast consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat Order: glass, juice, bowl, cereal, milk, spoon, eat.
Precedence Example Correctness Proof
z Show if (u, v) ∈ E, then f [v] < f [u]. z What if we started with juice? z When we explore (u, v), what are their colors? z Note, u is gray – we are exploring it 1 2 3 4 5 6 7 8 9 10 11 12 13 14 z Is v ggyray? z No, because then v would be an ancestor of u. z ⇒ (u, v) is a back edge. eat glass milk spoon z ⇒ a cycle (dag has no back edges). z Is v white? juice cereal z Then v becomes descendant of u. z By parenthesis theorem, d[u] < d[v] < f [v] < f [u]. bowl z Is v black? consider reverse order of finishing times: z Then v is already finished. spoon, bowl, cereal, milk, glass, juice, eat z Since we’re exploring (u, v), we have not yet finished u. z Therefore, f [v] < f [u]. Strongly Connected Components Uses of SCC’s
z Consider a directed graph. z Packaging software modules z A strongly connected component (SCC) z Construct directed graph of which modules call which other modules of the graph is a maximal set of nodes z A SCC is a set of mutually interacting with a (directed) path between every pair modules of nodes. z Pack together those in the same SCC z If a path from u to v exists in the SCC, then a path from v to u also exists. z Problem: Find all the SCCs of the graph.
SCC Example Main Idea of SCC Algorithm
z DFS tells us which nodes are reachable from the roots of the individual trees h f a e z Also need information in the other direction: is the root reachable from its g c b d descendants? z Run DFS again on the transpose graph
four SCCs (thditifthd)(reverse the directions of the edges) SCC Algorithm SCC Alggporithm Example
Input: directed graph G = (V ,E) h f a e 1. call DFS(G) to compute finishing times 2. computGte GT // t ranspose graph g c b d 3. call DFS(GT), considering nodes in decreasing order of finishing times
4. each tree from Steppp 3 is a separate input graph - run DFS SCC of G
After Step 1 After Step 2 fin(c) fin(f) fin(d) fin(b) fin(e) fin(a) fin(h) fin(g) h f a e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
c d h g c b d b e g a f transposed input graph - run DFS with specified order of Order of nodes for Step 3: f, g, h, a, e, b, d, c nodes After Step 3 Run Time of SCC Algorithm
z Step 1: O(V+E) to run DFS z Step 2: O(V+E) to construct transpose 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 graph, assuming adjacency list rep. z g Adjacency matrix is O(1) time w/ wrapper. h e c z SO()Step 3: O(V+E) to run DFS again f a b d z Step 4: O(V) to output result SCCs are {f,h,g} and {a,e} and {b,c} and {d}. z Total: O(V+E)
Component Graph Comppponent Graph Facts
SCC z GSCC = (VSCC, ESCC). z Claim: G is a directed acyygpclic graph. z Suppose there is a cycle in GSCC such that SCC z V has one vertex for each SCC in G. component Ci is reachable from component Cj and vice versa. SCC z E hdifth’dhas an edge if there’s an edge z Then Ci and Cj would not be separate SCCs. between the corresponding SCC’s in G. z Lemma: If there is an edge in GSCC from {}{a,e} component C' to component C, then f(C') > f(C). {f,h,g} z Consider any component C during Step 1 (running {d} DFS on G) z Let d(C) be earliest discovery time of any node in C SCC G based on example {b,c} graph from before z Let f(C) be latest finishing time of any node in C