Graph Representation (23.1/22.1) • HW: problem 23.3, p.496 • G=(V, E) -graph:

Download Report

Transcript Graph Representation (23.1/22.1) • HW: problem 23.3, p.496 • G=(V, E) -graph:

Graph Representation (23.1/22.1)
• HW: problem 23.3, p.496
• G=(V, E) -graph:
– V = V(G) - vertices;
– E = E(G) - edges (connecting pairs of vertices)
1
2
• Adjacency-list
1
2
3
4
5
3
3
5
5
2
3
4
5
• Adjacency-matrix
4
1
2
3
4
5
1
2
0
0
0
0
1
1
1
0
0
0
3
4
0
0
0
0
0
0
0
0
1
1
5
0
1
0
0
0
Depth-First Search (23.3/22.3)
• Methodically explore all vertices and edges
All vertices are White, t = 0
For each vertex u do if u is White then Visit(u)
Procedure Visit(u)
color u Gray; d[u]  t  t +1
for each v adjacent to u do
if v is White then Visit(v)
color u Black
f [u]  t  t +1
Gray vertices =
stack of recursive calls
Depth-First Search
1
5
3
6
8
4
2
7
Depth-First Search
1
1
5
3
6
8
4
2
7
Depth-First Search
1
5
1
3
6
8
4
2
2
7
Depth-First Search
1
5
1
3
6
8
3
4
2
2
7
Depth-First Search
1
5
1
3
6
8
3
4
2
2
7
Depth-First Search
1
5
1
3
6
8
3
4
2
2
7
Depth-First Search
1
5
1
3
6
8
3
4
2
2
7
Depth-First Search
1
5
1
6
4
3
8
3
4
2
2
7
Depth-First Search
1
5
1
6
4
3
8
3
4
2
2
7
Depth-First Search
1
5
1
6
4
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
6
4
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
6
4
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
8
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
8
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
8
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
8
7
Depth-First Search
1
5
1
7
5
4
6
6
3
8
3
4
2
2
8
7
Depth-First Search (23.3/22.3)
• Runtime of DFS = O(V+E)
– once per vertex
– once per edge
• Kinds of edges:
–
–
–
–
tree edge
back edge
forward edge
cross edge
(gray to gray)
(gray to gray)
(gray to black)
(gray to black)
• G is undirected  only tree and back
• Undirected G is acyclic  no back edges
– If a graph is acyclic can be found in O(V) time
Topological Sort (23.4/22.4)
• DAG = directed acyclic graph
– has levels or depth
– cannot return up
• Topological Sort(G)
– call DFS(G) to compute f[v]
– sort according to finishing times
• Directed graph G is acyclic  no back edges
Breadth-First Search (23.2/22.2)
• BFS discovers all vertices at distance k before any vertices
at distance k+1.
• Initialization
– assigns  to all vertices: w(v)= 
– color all white
• Color s gray, w(s)=0, enqueue s in Q
• for the head u of Q
– for each v adjacent to u,
• d[v]=d[u]+1
• enqueue v in Q
– dequeue Q
– color u black
Breadth-First Search
r
s
t
u








v
w
x
y
Breadth-First Search
r
s
t
u

0






v
w
x
y
Q
s
0
Breadth-First Search
r
s
t
u
1
0



1


v
w
x
y
Q
r w
1
1
Breadth-First Search
r
s
t
u
1
0


2
1


v
w
x
y
Q
w v
1
2
Breadth-First Search
r
s
t
u
1
0
2

2
1
2

v
w
x
y
Q
v t x
2
2
2
Breadth-First Search
r
s
t
u
1
0
2

2
1
2

v
w
x
y
Q
t
x
2
2
Breadth-First Search
r
s
t
u
1
0
2
3
2
1
2

v
w
x
y
Q
x u
2
3
Breadth-First Search
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
u y
3
3
Breadth-First Search
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
y
3
Breadth-First Search
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q

Breadth-First Search (23.2/22.2)
• Run-time = O(V+E)
• Shortest-path tree
– the final weight is the minimum distance
– keep predecessors and get the shortest path
– all BFS shortest paths make a tree