/
Introduction to Algorithms Introduction to Algorithms

Introduction to Algorithms - PowerPoint Presentation

karlyn-bohler
karlyn-bohler . @karlyn-bohler
Follow
384 views
Uploaded On 2016-02-21

Introduction to Algorithms - PPT Presentation

Graph Algorithms CSE 680 Prof Roger Crawfis Bipartiteness Graph G VE 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 ID: 225236

scc graph linked list graph scc list linked bipartite dfs step bowl nodes spoon fin juice eat milk component

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Introduction to Algorithms Graph Algorithms

CSE 680

Prof. Roger CrawfisSlide2

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 BAlternatively:Graph G = (V,E) is bipartite iff all its cycles have even lengthGraph G = (V,E) is bipartite iff nodes can be coloured using two coloursQuestion: given a graph G, how to test if the graph is bipartite?Note: graphs without cycles (trees) are bipartite

non bipartite

bipartite:Slide3

Testing bipartiteness

Method

: use BFS search tree

Recall: BFS is a rooted spanning tree.Algorithm:Run BFS search and colour all nodes in odd layers red, others blueGo 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 We use the following alternative definitions in the analysis:Graph G = (V,E) is bipartite iff all its cycles have even length, orGraph G = (V,E) is bipartite

iff it has no odd cycle

non bipartite

bipartiteSlide4

Topological Sort

Want to “sort”

or

linearize a directed acyclic graph (DAG).BED

C

A

C

E

D

A

BSlide5

Topological SortPerformed on a DAG.

Linear ordering of the vertices of

G

such that if (u, v)  E, then u appears before v.Topological-Sort (G)

call DFS(G) to compute finishing times f

[v] for all v  V

as each vertex is finished, insert it onto the front of a linked listreturn the linked list of vertices

Time:

(

V + E

).Slide6

Example

Linked List:

A

B

D

C

E

1/Slide7

Example

Linked List:

A

B

D

C

E

1/

2/Slide8

Example

Linked List:

A

B

D

C

E

1/

2/3

E

2/3Slide9

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

DSlide10

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/Slide11

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/

6/Slide12

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/

6/7

6/7

CSlide13

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/8

6/7

6/7

C

5/8

BSlide14

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/8

6/7

6/7

C

5/8

B

9/Slide15

Example

Linked List:

A

B

D

C

E

1/4

2/3

E

2/3

1/4

D

5/8

6/7

6/7

C

5/8

B

9/10

9/10

ASlide16

Precedence ExampleTasks that have to be done to eat breakfast:get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat.

Certain events must happen in a certain order (ex: get bowl before pouring milk)

For other events, it doesn't matter (ex: get bowl and get spoon)Slide17

Precedence Example

get glass

pour juice

get bowlpour cerealpour milk

get spooneat breakfast

Order: glass, juice, bowl, cereal, milk, spoon, eat.Slide18

Precedence ExampleTopological Sort

eat

juice

glass

milk

cereal

bowl

spoon

consider reverse order of finishing times:

spoon, bowl, cereal, milk, glass, juice, eat

1

2

3

4

5

6

7

8

9

10

11

12

13

14Slide19

Precedence ExampleWhat if we started with

juice?

eat

juice

glass

milk

cereal

bowl

spoon

consider reverse order of finishing times:

spoon, bowl, cereal, milk, glass, juice, eat

1

2

3

4

5

6

7

8

9

10

11

12

13

14Slide20

Correctness ProofShow

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 itIs v gray

?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?

Then v becomes descendant of u.

By parenthesis theorem, d[u] < d

[v] < f [

v] < f [u].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

].Slide21

Strongly Connected ComponentsConsider a directed graph.A strongly connected component (SCC) of the graph is a maximal set of nodes with a (directed) path between every pair of

nodes.

If a path from

u to v exists in the SCC, then a path from v to u also exists.Problem: Find all the SCCs of the graph.Slide22

Uses of SCC’sPackaging software modules

Construct

directed graph of which modules call which other modules

A SCC is a set of mutually interacting modulesPack together those in the same SCCSlide23

SCC Example

h

f

a

e

g

c

b

d

four SCCsSlide24

Main Idea of SCC AlgorithmDFS tells us which nodes are reachable from the roots of the individual treesAlso need information in the

other direction:

is the root reachable from its descendants?

Run DFS again on the transpose graph (reverse the directions of the edges)Slide25

SCC AlgorithmInput: directed graph G = (V,E)

call DFS(G) to compute finishing times

compute G

T // transpose graphcall DFS(GT), considering nodes in decreasing order of finishing timeseach tree from Step 3 is a separate SCC of GSlide26

SCC Algorithm Example

input graph - run DFS

h

f

a

e

g

c

b

dSlide27

After Step 1

c

b

g

a

f

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

d

e

h

fin(c)

fin(d)

fin(b)

fin(e)

fin(a)

fin(h)

fin(g)

fin(f)

Order of nodes for Step 3: f, g, h, a, e, b, d, cSlide28

After Step 2

transposed input graph - run DFS with specified order of nodes

h

f

a

e

g

c

b

dSlide29

After Step 3

g

h

e

f

a

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

d

SCCs are {f,h,g} and {a,e} and {b,c} and {d}.

b

cSlide30

Run Time of SCC AlgorithmStep 1: O(V+E) to run DFSStep 2: O(V+E) to construct transpose graph, assuming adjacency list rep

.

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

Step 3: O(V+E) to run DFS againStep 4: O(V) to output resultTotal: O(V+E)Slide31

Component GraphGSCC

=

(VSCC, ESCC).VSCC has one vertex for each SCC in G.ESCC has an edge if there’s an edge between the corresponding SCC’s in G. {a,e}

{f,h,g}

{d}

{b,c}

G

SCC

based

on example graph from beforeSlide32

Component Graph FactsClaim:

G

SCC

is a directed acyclic graph.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 f(C') > f(C).Consider any component C during Step 1 (running DFS on G)Let d(C) be earliest discovery time of any node in CLet f(C) be latest finishing time of any node in C