Upload
tamsin-cox
View
217
Download
0
Embed Size (px)
DESCRIPTION
21NIL Definitions and Representations Representations –Adjacency Matrix –Adjacency Lists –Adjacency Multi-lists Vertex
Citation preview
Graph and Digraph
Sung Yong ShinTC Lab.
CS Dept., KAIST
Contents
1. Definitions and Representations
2. A Minimum Spanning Tree Algorithm
3. A Shortest-Path Algorithm
4. Traversing Graphs and Digraphs
5. Biconnected Components of a Graph
6. Strongly Connected Components of a Digraph
21
NIL41
31
32
NIL42
NIL43 NIL
1. Definitions and Representations
• Representations– Adjacency Matrix– Adjacency Lists– Adjacency Multi-lists
2
1
4
3
1
234
Vertex
2. Min-Cost Spanning Tree
• Greedy (1) Sort E in the non-decreasing order of weights
O(eloge) n2logn Why? (2) Choose n–1 edges from the sorted list of E as long as the current selected edge does not form a cycle with the previously-chosen edges
O(e + eG(n)) Kruskal’s algorithm !!! Any Better way ?
31
2
1
1
4
2
3
11
62
G=(V, E, W)
weight
Why not local search ?
Observation
31 15
2082
9
F
G
A
BC
D
E
H
12
2
12
2
1 5
24
13
5
G = (V, E, W)
tree vertices = {A, B, C} = VT
fringe vertices = {E,F,D} = VF
unseen vertices = {G, H} = VU CE = {{A,D},{B,E},{C,F}}
Dijkstra / Prim Algorithm
D
A C
B
E
1
4 2
3
1 5
2
7
VT = { A }
VF = { C, B, D }
CE = { { A, C }, { A, B }, { A, D } }
4 31
D
A C
B
E
1
4 2
3
1 5
2
7
VT = { A, C }
VF = { B, D, E }
CE = { { A, D }, { C, B }, { C, E } }
2 73
D
A C
B
E
1
4 2
3
1 5
2
7
VT = { A, B, C }
VF = { D, E }
CE = { { B, D }, { B, E } } 1 5
D
A C
B
E
1
4 2
3
1 5
2
7
VT = { A, B, C, D }
VF = { E }
CE = { { D, E } }
D
A C
B
E
1
4 2
3
1 5
2
7
VT = { A, B, C, D, E }
VF = Ø
CE = Ø
D
A C
B
E
1
4 2
3
1 5
2
7
SI { Select an arbitrary vertex to start the tree } vT = Ø, vF = Ø, CE = Ø ; vT = vT {v} ; Update vF and CE ;
while vF Ø do ; e : = min - weight edge in CE ; Update vT, vF, CE ; end {while}
No sorting !!! Why this algorithm works ?
Lemma : Let G = ( V, E, W ) be a connected weighted graph and E' E be a subset of the edges in some minimum spanning tree T = ( V, ET ) for G. Let V' be the vertices incident with edges in E'. If { x, y } is an edge of minimum weight such that x V' and y V', then E' {x, y} ET.
[Proof]
x
y
vw
T' = (V', E' )T' T
T= (V, ET)
i) {x, y} ET trivialii) {x, y} ET
See the figure.
x
y
y
y
y
e
e
Algorithm : Minimum spanning tree(Dijkstra/Prim)Input : G = (V,E,W), a weighted graph.Output : The edges in a minimum spanning tree.1. { Initialization } Let x be an arbitrary vertex ; ET:= Ø; stuck := false ; VT:={x};
2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while VT V and not stuck do3. { Replace some candidate edges. }
for each fringe vertex y adjacent to x doif W(xy) < W(the candidate edge e incident with y) then
xy replaces e as the candidate edge for y ;end { if }
end{for} 4. { Find new fringe vertices and candidate edges. }
for each unseen y adjacent to x doy is now a fringe vertex and xy is a candidate ;
end { for } ;5. { Ready to choose next edge. }
if there are no candidates then stuck := true { no spanning tree} ;else
6. { Choose next edge. }Find a candidate edge e, with minimum weight ;x := the fringe vertex incident with e.Add x and e to the tree.{ x and e are no longer fringe and candidate. }
end { if }end { while }
Update candidate edges
Add new fringe verticesand edges. (candidate)
Choose new edges.O(n2)
InitializeO(1)
O(║E║)
Time complexity
Kruskal Dijkstra & Prim
))(log( neGeeO
)neO 2(
n)neO log(
[proof] ( By induction on # of edges chosen )
( m = 1 )
( m = k ) Assume that all the edges so far selected are in a min-cost spanning tree for G.
( m = k+1) By the previous Lemma, the result follows.
Theorem : Dijkstra/Prim Algorithm correctly constructs a min-cost spanning tree.
min-cost edge
Algorithm : Minimum spanning tree(Dijkstra/Prim)
1. { Initialization } Let x be an arbitrary vertex ; ET:= Ø ; stuck := false ; VT:={x} ; 2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. } while VT V and not stuck do3. { Replace some candidate edges. } for each fringe vertex y adjacent to x do if W(xy) < W(the candidate edge e incident with y) then xy replaces e as the candidate edge for y ; end { if } end{for} 4. { Find new fringe vertices and candidate edges. } for each unseen y adjacent to x do y is now a fringe vertex ; xy is now a candidate ; end { for } ; 5. { Ready to choose next edge. } if there are no candidates then stuck := true { no spanning tree} ; else6. { Choose next edge. } Find a candidate edge e, with minimum weight ; x := the fringe vertex incident with e. Add x and e to the tree. { x and e are no longer fringe and candidate. } end { if } end { while }
O(1)
O(|E|)
O(n2)
O(|E|+n2)
2
73
3A intree B nil 2 A intree C F 4 B fringe D unseen E unseen F nil 7 A fringe G F 3 A intree H C 3 G fringe I H 1 G fringe
fringeLink parent adjacencyList fringeWgt status
Shaded entries in fringeLink areno longer in use. fringeList = I.
[ Minimum spanning tree data structure ]
A
FI
G
H
E D
A
GH
B
I
F
C
B
C
2
3
4
7
1
3
6
5 16 4
2
2
1
24
8
(Adjacency lists are not shown. Nodes are assumed to be in alphabetical order within each list.)
Lower Bound (|E|) Why ? (1) a trivial lower bound O(|V| + |E|) = O(|E|) (2) adversary argument
Every edge is required to be examined at least once. Why ? Suppose that some edge e is not examined at all. (see below)
e must not be contained in a min-cost spanning tree. why ? Now, w(e) = 1 Contradiction ! why ? (|E|)
3 2e12
144
5 9
1510
1112 7
G = (V, E)w(f) 2 fE{e}
3. A shortest path algorithm
P : Given G = (V, E, W) and s,d V, find a shortest path between s and d.
Does the Dijkstra/Prim algorithm also work for solving the shortest path finding problem? No !!!
Why ?
Dijkstra’s Algorithm
minTj
Step 0 { initialization } v1 := 0 vj := w1j, j = 2, 3, 4,···, n p := {1}, T := {2, 3, 4,···, n} Step 1 { Designation of permanent of label } find k such that vk = { vj } T := T \{k} P := P {k} if T = Ø , stop ( if k = d, stop ) Step 2 { Revision of Tentative label } vj := min{ vj, vk + wkj ) j T go to step 1.
3
421 1
3
22
s 1 d 4w12 = 1, w23 = 3, w24 = 2, w34 =2
v1 := 0, v2 = 1, v3 = , v4 = P := {1}, T := {2, 3, 4}min{vj} = min{v2, v3, v4}
k=2 Is k=4 ? No !!!
T := {2,3,4}/{2} = {3,4}P := P {2} = {1,2}v3 = min{v3, v2+w23} = min{, 1+3} = 4v4 = min{v4, v2+w24} = min{, 1+2} = 3min{vj} = min{v3, v4} = min{4, 3} = v4
k = 4
jT 1
jT
T := {3,4}/{4} = {3}P := P {4} = {1,2,4}
Is k=4 ? Yes !!!
Why does Dijkstra’s Algorithm Work ?
s
emin
xy
x'
y'
4. Traversing Graphs and Digraphs
8 Queen Problem
(5,4) (5,8) (5,2) (5,4) (5,3) (5,4)
(7,6)
(6,4)(6,4)
(7,6)
(4,8)(4,7)(4,2)
(3,5) (3,6)
(2,7)(2,6)(2,5)(2,4)(2,3) (2,8)
(1,1) (1,2) (1,3)(1,4) (1,5) (1,6) (1,7) (1,8)
start
Recursion
Depth First Search
Q
Q
Q
Q
Q
· · · · ···· · · ··
· · ·· ·
· (3,7) (3,8)
A
B C
C
1
1 1 2
1 2
AB
C12
1C
12
1
ABC1
C112
12
Depth First Search Breadth First Search
C
HDB E G
FA
DFS tree
procedure DFS (AdjList : HeaderList; v : VertexType) var s: Stack w: VertexType begin s:= Ø visit, mark, and stack v; while s!= Ø do while there are unmarked vertex w adjacent to Top(s) do visit, mark, and stack w end{while}; pop s; end{outer while} end{DFS}
H
B
G
E
F
A
C D6
5
7
321
4
561
4 72
C
HDB E G
FA
3
Depth First Search
26
7
51
34
Breadth First Search
A
C7
1 2
3 4 5 6
BFS tree
procedure BFS(AdjList : HeaderList; v : VertexType) var Q: Queue w: VertexType begin Q:= Ø visit and mark v; insert v in Q; while Q Ø do x := Front(Q); for each unmarked vertex w adjacent to x do visit and mark w; insert w in Q; end{for}; end{while} end{BFS}
C
HDB E G
FA
D E G
H
FB
Depth First Search
undirected graphs
Tree edges Back edges (no other types)
E
HIF G C
BA
Depth First Search Tree ( Directed Graph )
A
G I
H
C
B F
E
71
2
3
45
6
Tree edgesBack edgesCross edgesDescendant edges (forward)
why not in undirected graph?
Note : (1) top down (2) left right
• How to process vertices– first visited ①– back up to a vertex ②– back up from a vertex ③
• How to process edges– tree edges– back edges– descendant edges– cross edges
A Generalized Depth-first Search Skeleton
v① ③
②
Depending on applications, we have to take proper action for each of cases.
Input: G=(V,E), a graph or digraph represented by the adjacency list structure described with V={1,2,…,n} var mark : array[VertexType] of integer; markValue : integer; procedure DFS(v: VertexType); {Does a depth-first search beginning at the vertex v, marking the vertices with markValue.} var w: VertexType; ptr: NodePointer; begin {process vertex when first encountered (like preorder).} mark[v] := markValue; ptr := adjacencyList[v]; while ptr nil do w := ptr.vertex {Processing for every edge. (If G is undirected, each edge is encountered twice; an algorithm may have to distinguish the two encounters.)} if mark[w] = 0 {unmarked} then {Processing for tree edges, vw.}vertex link DFS(w); {Processing when backing up to v (like inorder) } else {Processing for nontree edges. (If G is undirected, an algorithm may have to distinguish the case where w is the parent fo v.) } end{if} ptr := ptr.link end{while} {Processing when backing up from v (like postorder) } end{DFS}
Algorithm : General Depth-first Search Skeleton
B
D E
A
C
B
D E
A
C
AB
C
BC
A
CA
B
D
E
D
E
G=(V,E) Adjaciency Lists DSF Tree
back edge
tree edge
C
C
1
2
3
4
1
2, 43
2