Graph Representation (23.1/22.1) - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Graph Representation (23.1/22.1)

Description:

Title: Analysis and Design of Algorithms Author: alexz Last modified by: cscxkcx Created Date: 4/5/1998 4:39:57 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 41
Provided by: ALE7150
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Graph Representation (23.1/22.1)


1
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
3
4
5
  • Adjacency-list
  • Adjacency-matrix

1
3
4
2
3
3
5
4
5
5
2
2
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 du ? 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

3
Depth-First Search
4
Depth-First Search
1
5
Depth-First Search
1
2
2
6
Depth-First Search
1
3
2
2
7
Depth-First Search
1
3
2
2
8
Depth-First Search
1
3
2
2
9
Depth-First Search
1
3
2
10
Depth-First Search
1
4
3
2
11
Depth-First Search
1
4
3
2
12
Depth-First Search
1
4
3
2
13
Depth-First Search
5
1
4
3
2
14
Depth-First Search
5
1
4
3
2
15
Depth-First Search
5
1
4
6
3
2
16
Depth-First Search
5
1
4
6
3
2
17
Depth-First Search
5
1
4
6
3
2
18
Depth-First Search
5
1
4
6
3
2
19
Depth-First Search
5
1
4
6
3
2
20
Depth-First Search
5
7
1
4
6
3
2
21
Depth-First Search
5
7
1
4
6
3
2
22
Depth-First Search
5
7
1
4
6
3
8
2
23
Depth-First Search
5
7
1
4
6
3
8
2
24
Depth-First Search
5
7
1
4
6
3
8
2
25
Depth-First Search
5
7
1
4
6
3
8
2
26
Depth-First Search
5
7
1
4
6
3
8
2
27
Depth-First Search (23.3/22.3)
  • Runtime of DFS O(VE)
  • once per vertex
  • once per edge
  • Kinds of edges
  • tree edge (gray to gray)
  • back edge (gray to gray)
  • forward edge (gray to black)
  • cross edge (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

28
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 fv
  • sort according to finishing times
  • Directed graph G is acyclic ? no back edges

29
Breadth-First Search (23.2/22.2)
  • BFS discovers all vertices at distance k before
    any vertices at distance k1.
  • 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,
  • dvdu1
  • enqueue v in Q
  • dequeue Q
  • color u black

30
Breadth-First Search
r
s
t
u
x
v
w
y
31
Breadth-First Search
r
s
t
u
x
v
w
y
Q
s
0
32
Breadth-First Search
r
s
t
u
x
v
w
y
Q
r
w
1
1
33
Breadth-First Search
r
s
t
u
x
v
w
y
Q
w
v
1
2
34
Breadth-First Search
r
s
t
u
x
v
w
y
Q
v
t
x
2
2
2
35
Breadth-First Search
r
s
t
u
x
v
w
y
Q
t
x
2
2
36
Breadth-First Search
r
s
t
u
x
v
w
y
Q
x
u
2
3
37
Breadth-First Search
r
s
t
u
x
v
w
y
Q
u
y
3
3
38
Breadth-First Search
r
s
t
u
x
v
w
y
Q
y
3
39
Breadth-First Search
r
s
t
u
x
v
w
y
Q
?
40
Breadth-First Search (23.2/22.2)
  • Run-time O(VE)
  • Shortest-path tree
  • the final weight is the minimum distance
  • keep predecessors and get the shortest path
  • all BFS shortest paths make a tree
Write a Comment
User Comments (0)
About PowerShow.com