Docsity

Examples on Graphs and Digraphs - Slides | MCS 401, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Graphs and Digraphs — Examples
An (undirected) graph G = (V,E)
A
B
C
D
E
F
A
F
B E
C
D
A
B C D E F
A
B
C
D
E
F
0 1 0 1 0 1
1 0 0 0 1 0
0 0 0 1 0 0
1 0 1 0 1 1
0 1 0 1 0 0
1 0 0 1 0 0
B D F
A E
D
A C
B D
E
A D
F
adjacency matrix for G
adjacency
list for G
n vertices, e edges (0 ≤ e ≤ n(n–1)/2 ≈ n2/2).
Adjacency matrix: Θ(n2) space. An algorithm that examines the
entire graph structure will require Ω(n2) time.
Adjacency list: Θ(n+e) space. An algorithm that examines the
entire graph structure will require Ω(n+e) time.
Often
,
e << n2. In this case
,
the ad
j
acenc
y
list ma
y
be
p
referable.
pf3
pf4
pf5

Partial preview of the text

Download Examples on Graphs and Digraphs - Slides | MCS 401 and more Study notes Algorithms and Programming in PDF only on Docsity!

Graphs and Digraphs — Examples

An (undirected) graph G = ( V , E )

A B C D E F

A

F

B E

C

D

A B C D E F A B C D E F 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 0 1 0 0

B D F

A E

D

A C

B D

E

A D

F

adjacency matrix for G

adjacency list for G

n vertices, e edges (0 ≤ e ≤ n ( n –1)/2 ≈ n^2 /2).

Adjacency matrix: Θ( n^2 ) space. An algorithm that examines the

entire graph structure will require Ω( n^2 ) time.

Adjacency list: Θ( n+e ) space. An algorithm that examines the

entire graph structure will require Ω( n+e ) time.

Often, e << n^2. In this case, the adjacency list may be preferable.

A digraph G = ( V , E )

A B C D E F

A B C D E F A B C D E F 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0

B F

E

A C

B D

D

adjacency matrix for G

adjacency list for G

In a digraph, e may be as high as n ( n –1) ≈ n^2 , but otherwise the

remarks on the previous page hold.

A

F

B

C

D

E

Directed Acyclic Graphs (DAGs)

In any digraph , we define a vertex v to be

a source, if there are no edges leading into v , and

a sink if there are no edges leading out of v.

A directed acyclic graph (or DAG) is a digraph that has no cycles.

Example of a DAG:

Theorem Every finite DAG has at least one source, and at least one

sink.

In fact, given any vertex v , there is a path from some

source to v , and a path from v to some sink.

A

F

B

C

D

E

a sink G

a source

Note: This theorem need not hold in an infinite DAG. For example, this DAG has neither a source nor a sink.

Note: In any digraph, the vertices could represent tasks, and the edges

could represent constraints on the order in which the tasks be

performed.

For example, A must be performed before B, F, or G.

B must be performed before C or E.

C must be performed before G.

D must be performed before C.

E must be performed before D.

F must be performed before D.

We will see that the constraints are consistent if any only if the digraph has no cycles, i.e., is a DAG.

A topological sort of a digraph G = (V,E) is labeling of the vertices by 1, 2, ..., |V| (or by elements of some other ordered set) such that

( u , v ) is a edge ⇒ label( u ) < label( v ).

We will see that a digraph has a topological sort if and only if it is a DAG.

For a tasks / constraints graph, a topological sort provides an order in which the tasks can be performed serially, and conversely any valid order for performing the tasks serially gives a topological sort.

... (^) -3 -2 -1 0 1 2 3 ...

Given a strongly connected digraph G, we may form the component digraph GSCC^ as follows:

i) The vertices of GSCC^ are the strongly connect components of G. ii) There is an edge from v to w in G SCC^ if there is an edge from some vertex of component v to some vertex of component w in G.

Theorem: The component graph of a digraph is a DAG.

Here is the component digraph for the digraph on the preceding page.