CPSC 311 Analysis of Algorithms. Graph Algorithms Prof. Jennifer Welch Fall 2009. a. c. d. b. e. Adjacency List Representation. b. c. a. b. a. d. e. c. a. d. d. b. c. e. e. b. d. Space-efficient for sparse graphs. a. c. d. b. e. Adjacency Matrix Representation.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
CPSC 311Analysis of Algorithms
Graph Algorithms
Prof. Jennifer Welch
Fall 2009
CPSC 311, Fall 2009
a
c
d
b
e
b
c
a
b
a
d
e
c
a
d
d
b
c
e
e
b
d
Space-efficient for sparse graphs
CPSC 311, Fall 2009
a
c
d
b
e
a b c d e
0
a
1
1
0
0
b
1
0
0
1
1
c
1
0
0
1
0
d
0
1
1
0
1
e
0
1
0
1
0
Check for an edge in constant time
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
a
c
d
b
s
CPSC 311, Fall 2009
CPSC 311, Fall 2009
(continued on next slide)
CPSC 311, Fall 2009
(continued from previous slide)
CPSC 311, Fall 2009
a
c
d
b
s
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
a
c
d
b
s
d = 1
d = 2
d = 0
d = 2
d = 1
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
u
c
w
v
s
dist=x+1
dist=x-1
dist=x
dist=x-1
CPSC 311, Fall 2009
d[u] + 1= x
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
a
b
c
d
e
f
g
h
CPSC 311, Fall 2009
a
d
b
c
e
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
G = (V,E) w.r.t. DFS forest
CPSC 311, Fall 2009
tree
a
d
b
f
tree
c
e
tree
back
back
tree
in DFS forest
not in DFS forest
forward
cross
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
get glass
get bowl
pour juice
pour cereal
get spoon
pour milk
eat breakfast
Order: glass, juice, bowl, cereal, milk, spoon, eat.
CPSC 311, Fall 2009
CPSC 311, Fall 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
eat
milk
spoon
juice
cereal
glass
bowl
consider reverse order of finishing times:
spoon, bowl, cereal, milk, glass, juice, eat
CPSC 311, Fall 2009
input: DAG G = (V,E)
1. call DFS on G to compute finish[v] for all nodes v
2. when each node's recursive call finishes, insert it on the front of a linked list
3. return the linked list
Running Time: O(V+E)
CPSC 311, Fall 2009
Case 1: v is finished when u is discovered. Then v finishes before u finishes.
Case 2: v is not yet discovered when u is discovered.
Claim: v will become a descendant of u and thus v will finish before u finishes.
Case 3: v is discovered but not yet finished when u is discovered. Show not possible…
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
h
f
a
e
g
c
b
d
four SCCs
CPSC 311, Fall 2009
from http://www.cs.princeton.edu/courses/archive/fall07/cos226/lectures.html
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
input: directed graph G = (V,E)
CPSC 311, Fall 2009
h
f
a
e
g
c
b
d
input graph - run DFS
CPSC 311, Fall 2009
h
c
d
e
b
g
a
f
fin(c)
fin(f)
fin(d)
fin(b)
fin(e)
fin(a)
fin(h)
fin(g)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Order of nodes for Step 3: f, g, h, a, e, b, d, c
CPSC 311, Fall 2009
h
f
a
e
g
c
b
d
transposed input graph - run DFS with specified order of nodes: f, g, h, a, e, b, d, c
CPSC 311, Fall 2009
g
h
c
e
f
d
a
b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SCCs are {f,h,g} and {a,e} and {b,c} and {d}.
CPSC 311, Fall 2009
Assume adjacency list representation.
CPSC 311, Fall 2009
CPSC 311, Fall 2009
{a,e}
{f,h,g}
{d}
{b,c}
based on example graph from before
CPSC 311, Fall 2009
CPSC 311, Fall 2009
f(C') > f(C).
CPSC 311, Fall 2009
C'
C
CPSC 311, Fall 2009
C'
C
CPSC 311, Fall 2009
CPSC 311, Fall 2009
CPSC 311, Fall 2009
w
z
u
w
z
u
C
G:
C
GT:
C'
C'
CPSC 311, Fall 2009
w
z
u
G:
C
C'
CPSC 311, Fall 2009