Graph Algorithms Graph Algorithms g Graph G = (V,E) is bipartite - - PowerPoint PPT Presentation

graph algorithms graph algorithms g
SMART_READER_LITE
LIVE PREVIEW

Graph Algorithms Graph Algorithms g Graph G = (V,E) is bipartite - - PowerPoint PPT Presentation

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 Introduction to Algorithms end in B end in B Alternatively:


slide-1
SLIDE 1

Introduction to Algorithms Introduction to Algorithms

Graph Algorithms Graph Algorithms g

CSE 680

  • Prof. Roger Crawfis

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 end in B end in B Alternatively:

  • 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 Question: given a graph G how to test if the graph is bipartite? Question: given a graph G, how to test if the graph is bipartite? Note: graphs without cycles (trees) are bipartite bipartite: non bipartite p

Testing bipartiteness

Method: use BFS search tree Recall: BFS is a rooted spanning tree. Algorithm: Algorithm:

  • 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 colours at its ends if so then G is bipartite, otherwise it is not 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

Graph G (V,E) is bipartite iff it has no odd cycle

bi tit non bipartite bipartite

Topological Sort p g

Want to “sort” or linearize a directed acyclic graph (DAG). y g p ( )

B D A E C E C C E D A B

slide-2
SLIDE 2

Topological Sort p g

Performed on a DAG. Performed on a DAG. Linear ordering of the vertices of G such that if

(u, v) ∈ E, then u appears before v. ( , ) , pp

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 3. return the linked list of vertices Time: Θ(V + E).

Example p

A B D 1/ Linked List: C E Linked List:

Example p

A B D 1/ 2/ Linked List: C E Linked List:

Example p

A B D 1/ 2/3 Linked List: C E Linked List: E 2/3 E

slide-3
SLIDE 3

Example p

A B D 1/4 2/3 Linked List: C E Linked List: E 2/3 1/4 D E D

Example p

A B D 1/4 5/ 2/3 Linked List: C E Linked List: E 2/3 1/4 D E D

Example p

A B D 1/4 5/ 2/3 6/ Linked List: C E Linked List: E 2/3 1/4 D E D

Example p

A B D 1/4 5/ 2/3 6/7 Linked List: C E Linked List: E 2/3 1/4 D 6/7 C E D C

slide-4
SLIDE 4

Example p

A B D 1/4 5/8 2/3 6/7 Linked List: C E Linked List: E 2/3 1/4 D 6/7 C 5/8 B E D C B

Example p

A B D 1/4 5/8 9/ 2/3 6/7 Linked List: C E Linked List: E 2/3 1/4 D 6/7 C 5/8 B E D C B

Example p

A B D 1/4 5/8

9/10

2/3 6/7 Linked List: C E Linked List: E 2/3 1/4 D 6/7 C 5/8 B

9/10

A E D C B A

Precedence Example p

Tasks that have to be done to eat Tasks that have to be done to eat

breakfast:

get glass pour juice get bowl pour cereal get glass, pour juice, get bowl, pour cereal,

pour milk, get spoon, eat.

Certain events must happen in a certain Certain events must happen in a certain

  • rder (ex: get bowl before pouring milk)

F th t it d 't tt (

For other events, it doesn't matter (ex:

get bowl and get spoon)

slide-5
SLIDE 5

Precedence Example p

get glass pour juice get bowl pour juice pour cereal ilk get spoon pour milk get spoon eat breakfast Order: glass, juice, bowl, cereal, milk, spoon, eat.

Precedence Example p

Topological Sort Topological Sort

1 2 3 4 5 6 7 8 9 10 11 12 13 14 eat milk spoon 1 2 3 4 5 6 7 8 9 10 11 12 13 14 juice glass cereal bowl consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat

Precedence Example p

What if we started with juice? What if we started with juice?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 eat glass milk spoon 1 2 3 4 5 6 7 8 9 10 11 12 13 14 juice cereal bowl consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat

Correctness Proof

Show if (u, v) ∈ E, then f [v] < f [u]. When we explore (u, v), what are their colors?

Note, u is gray – we are exploring it Is v gray?

g y

No, because then v would be an ancestor of u. ⇒ (u, v) is a back edge. ⇒ a cycle (dag has no back edges).

Is v white? Is v white?

Then v becomes descendant of u. By parenthesis theorem, d[u] < d[v] < f [v] < f [u].

Is v black? Is v black?

Then v is already finished. Since we’re exploring (u, v), we have not yet finished u. Therefore, f [v] < f [u].

slide-6
SLIDE 6

Strongly Connected Components

Consider a directed graph Consider a directed graph. A strongly connected component (SCC)

  • f the graph is a maximal set of nodes
  • f the graph is a maximal set of nodes

with a (directed) path between every pair

  • f nodes
  • f nodes.

If a path from u to v exists in the SCC, then

a path from v to u also exists a path from v to u also exists.

Problem: Find all the SCCs of the graph.

Uses of SCC’s

Packaging software modules Packaging software modules

Construct directed graph of which modules

call which other modules

A SCC is a set of mutually interacting

modules

Pack together those in the same SCC

SCC Example p

h f a e g c b d g c b d four SCCs

Main Idea of SCC Algorithm g

DFS tells us which nodes are reachable DFS tells us which nodes are reachable

from the roots of the individual trees

Also need information in the other Also need information in the other

direction: is the root reachable from its descendants? descendants?

Run DFS again on the transpose graph

( th di ti f th d ) (reverse the directions of the edges)

slide-7
SLIDE 7

SCC Algorithm g

Input: directed graph G = (V E) Input: directed graph G = (V,E)

  • 1. call DFS(G) to compute finishing times

t GT // t h

  • 2. compute GT // transpose graph
  • 3. call DFS(GT), considering nodes in

decreasing order of finishing times

  • 4. each tree from Step 3 is a separate

p p SCC of G

SCC Algorithm Example g p

h f a e g c b d input graph run DFS input graph - run DFS

After Step 1 p

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 fin(c) fin(d) fin(b) fin(e) fin(a) fin(h) fin(g) fin(f) c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 d h b g a f e Order of nodes for Step 3: f, g, h, a, e, b, d, c

After Step 2 p

h f a e g c b d transposed input graph run DFS with specified order of g c b d transposed input graph - run DFS with specified order of nodes

slide-8
SLIDE 8

After Step 3 p

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 g 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 h e f a d b c SCCs are {f,h,g} and {a,e} and {b,c} and {d}.

Run Time of SCC Algorithm g

Step 1: O(V+E) to run DFS Step 1: O(V+E) to run DFS Step 2: O(V+E) to construct transpose

graph assuming adjacency list rep graph, assuming adjacency list rep.

Adjacency matrix is O(1) time w/ wrapper.

S O( ) S

Step 3: O(V+E) to run DFS again Step 4: O(V) to output result Total: O(V+E)

Component Graph p p

GSCC = (VSCC ESCC) G

(V , E ).

VSCC has one vertex for each SCC in G.

ESCC h d if th ’ d

ESCC has an edge if there’s an edge

between the corresponding SCC’s in G.

{ } {a,e} {f,h,g} {d} {d} {b,c} GSCC based on example graph from before graph from before

Component Graph Facts p p

Claim: GSCC is a directed acyclic graph.

y g p

Suppose there is a cycle in GSCC such that

component Ci is reachable from component Cj and vice versa.

Then Ci and Cj would not be separate SCCs.

Lemma: If there is an edge in GSCC from

component C' to component C then component C to component C, then f(C') > f(C).

Consider any component C during Step 1 (running

DFS on G) DFS on G)

Let d(C) be earliest discovery time of any node in C Let f(C) be latest finishing time of any node in C