csc 331: algorithm analysis decompositions of graphs

27
CSC 331: Algorithm Analysis Decompositions of Graphs

Upload: linda-charles

Post on 12-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 331: Algorithm Analysis Decompositions of Graphs

CS

C 3

31

: A

lgori

thm

An

aly

sis

Decompositions of Graphs

Page 2: CSC 331: Algorithm Analysis Decompositions of Graphs

A wide range of problems can be expressed with clarity and precision in the concise pictorial

language of graphs.

Consider the task of coloring a political map:

What is the minimum number of colors needed, with the obvious

restriction that neighboring countries should have different

colors?

Page 3: CSC 331: Algorithm Analysis Decompositions of Graphs

The precise goal is now to assign a color to each vertex so that no edge has endpoints of the same

color.

2233

4455

66

88

77

99

1313

1111

1212

1010

11

Page 4: CSC 331: Algorithm Analysis Decompositions of Graphs

Formally, a graph is specified by a set of vertices V and by edges E between select pairs

of vertices.

V = {1, 2, 3, ..., 13}

2233

4455

66

88

77

99

1313

1111

1212

1010

11E = {{1, 2}, {1, 3},

{1, 4},..., {11, 12}, {11,

13}}

This is an undirected graph.

Page 5: CSC 331: Algorithm Analysis Decompositions of Graphs

We can represent a graph by an adjacency matrix:

if there are |V| vertices v1, ..., vn, this is an n × n array whose (i, j)th

entry is

For undirected graphs, the matrix is symmetric since an edge {u, v} can be taken in either

direction.

Page 6: CSC 331: Algorithm Analysis Decompositions of Graphs

We can represent a graph by an adjacency list:

It consists of |V| linked lists, one per vertex.

The linked list for vertex u holds the names of vertices to which u has an

outgoing edge.

Page 7: CSC 331: Algorithm Analysis Decompositions of Graphs

Adjacency matrix:

-The presence of a particular edge can be checked in constant time.

-The matrix takes up O(V2) space, which is wasteful if the graph does not have very many edges.

Adjacency list:

-The presence of a particular edge can no longer be checked in constant time.

-The total size of the data structure is O(|E|).

Page 8: CSC 331: Algorithm Analysis Decompositions of Graphs

CS

C 3

31

: A

lgori

thm

An

aly

sis

Depth-First Search in Undirected Graphs

Page 9: CSC 331: Algorithm Analysis Decompositions of Graphs

Chalk prevents looping.

String always takes you back to the starting place, enabling you to return to

passages that you previously saw but did not investigate.

Page 10: CSC 331: Algorithm Analysis Decompositions of Graphs

chalk == Boolean variable for each location

string == stack (recursion)

Page 11: CSC 331: Algorithm Analysis Decompositions of Graphs

What parts of a graph are reachable from a given vertex?

AA BB EE

II JJGG HH CC FF

KK LL

DD

Page 12: CSC 331: Algorithm Analysis Decompositions of Graphs

procedure explore(G, v) Input: G = (V, E) is a graph; v V∈Output: visited(u) is set to true for all nodes u reachable from v

visited(v) = trueprevisit(v)for each edge (v, u) E:∈

if not visited(u): explore(G, u)

postvisit(v)

Page 13: CSC 331: Algorithm Analysis Decompositions of Graphs

Starting at A, can we reach J?

AA BB EE

II JJGG HH CC FF

KK LL

DD

Starting at A, can we reach L?

Page 14: CSC 331: Algorithm Analysis Decompositions of Graphs

procedure dfs(G)for all v V:∈

visited(v) = false

for all v V:∈if not visited(v):

explore(v)

Page 15: CSC 331: Algorithm Analysis Decompositions of Graphs

What is the running time of DFS?

Each vertex is explore’d just once.

During the exploration of a vertex:

Some fixed amount of work (marking the spot as visited and the

pre/postvisit).A loop in which adjacent edges are

scanned, to see if they lead somewhere new.

Page 16: CSC 331: Algorithm Analysis Decompositions of Graphs

What is the running time of DFS?

This loop takes a different amount of time for each vertex, so let’s consider all vertices

together.Some fixed amount of work (marking

the spot as visited and the pre/postvisit).

A loop in which adjacent edges are scanned, to see if they lead somewhere

new.

O(|V|) - Constant amount of work per vertex

O(|E|) - Each edge {x, y} ∈ E is examined exactly twice.

Page 17: CSC 331: Algorithm Analysis Decompositions of Graphs

What is the running time of DFS?

O(|V| + |E|)

Linear in the size of its input.

Page 18: CSC 331: Algorithm Analysis Decompositions of Graphs

CS

C 3

31

: A

lgori

thm

An

aly

sis

Connectivity in Undirected Graphs

Page 19: CSC 331: Algorithm Analysis Decompositions of Graphs

An undirected graph is connected if there is a path between any pair of vertices.

AA BB EE

II JJGG HH CC FF

KK LL

DD

Page 20: CSC 331: Algorithm Analysis Decompositions of Graphs

What additional information would DFS give us if we defined pre/postvisit as

follows?

procedure previsit(v)pre[v] = clockclock = clock + 1

procedure postvisit(v)post[v] = clockclock = clock + 1

Page 21: CSC 331: Algorithm Analysis Decompositions of Graphs

CS

C 3

31

: A

lgori

thm

An

aly

sis

Decompositions of Directed Graphs

Page 22: CSC 331: Algorithm Analysis Decompositions of Graphs

DD

GG HH

AA CCBB

FFEE

Our DFS algorithm can be run verbatim on directed graphs, taking care to

traverse edges only in their prescribed directions.

Page 23: CSC 331: Algorithm Analysis Decompositions of Graphs

DD

GG HH

AA CCBB

FFEE

AA 1

BB 2

EE 3

FF 4

GG 5,6

,7 HH 8,9

,10

,11 CC 12

DD 13,14

,15

,16

Page 24: CSC 331: Algorithm Analysis Decompositions of Graphs

DD

GG HH

AA CCBB

FFEE

AA 1

BB 2

EE 3

FF 4

GG 5,6

,7 HH 8,9

,10

,11 CC 12

DD 13,14

,15

,16Other edges are either Back, Forward, or Cross edges:

F

F

BB

C

C

Page 25: CSC 331: Algorithm Analysis Decompositions of Graphs

Other edges (u,v) are either Back, Forward, or Cross edges:

Forward edge: pre(u) < pre(v) < post(v) < post(u)(Same as tree edge, but unvisited)

Back edge: pre(v) < pre(u) < post(u) < post(v)

Cross edge: pre(v) < post(v) < pre(u) < post(u)

Page 26: CSC 331: Algorithm Analysis Decompositions of Graphs

A cycle in a directed graph is a circular path.

v0 → v1 → v2 → … → vk → v0

A graph without cycles is acyclic.What algorithm tells if a graph is acyclic?

DD

GG HH

AA CCBB

FFEE

AA CC

DD

Page 27: CSC 331: Algorithm Analysis Decompositions of Graphs

Direct acyclic graphs (dags) are quite common, especially in modeling

relationships.

For DAGs, we often want to order the nodes linearly (topological sort). All edges are

forward edges.

DD

GG HH

AA CCBB

FFEE

BBAA CC EEDD FF GGHH

What algorithm will give a topological sort?What is the running time?