Algorithm Design & Analysis

Download Report

Transcript Algorithm Design & Analysis

Graph and Digraph
Sung Yong Shin
TC 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
1. Definitions and Representations
• Representations
– Adjacency Matrix
– Adjacency Lists
– Adjacency Multi-lists
1
2
1
3
1
4
2
3
2
4
NIL
3
4
NIL
Vertex
1
1
2
3
2
4
NIL
4
3
NIL
2. Min-Cost Spanning Tree
2
1
1
4
6
3
2 1
3
2
1
G=(V, E, W)
weight
1

• 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 ?
Observation
3
1
15
20
2
9
8
Why not local search ?
A
2
1
1
B
5
C
2
G = (V, E, W)
D
2
4
2
5
F
H
3
1
E
2
1
G
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
1
A
2
4
3
7
B
1
D
C
5
2
E
1
A
2
4
3
C
7
B
1
D
5
2
E
VT = { A }
VF = { C, B, D }
CE = { { A, C }, { A, B }, { A, D } }
3
1
4
1
A
2
4
3
C
7
B
1
D
5
2
E
VT = { A, C }
VF = { B, D, E }
CE = { { A, D }, { C, B }, { C, E } }
7
3
2
1
A
2
4
3
C
1
D
7
B
5
2
E
VT = { A, B, C }
VF = { D, E }
CE = { { B, D }, { B, E } }
1
5
1
A
2
4
3
C
7
B
1
D
5
2
VT = { A, B, C, D }
VF = { E }
CE = { { D, E } }
E
1
A
2
4
3
7
B
1
D
C
5
2
E
VT = { A, B, C, D, E }
VF = Ø
CE = Ø
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]
T' = (V', E' )
y
T= (V, ET)
x
v
T'  T
w
i) {x, y}  ET trivial
ii) {x, y}  ET
See the figure.
y
e
y
x
e
y
y
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 do
3.
{ 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 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 }
Initialize
O(1)
Update candidate edges
O(║E║)
Add new fringe vertices
and edges. (candidate)
Choose new edges.
O(n2)
Time complexity
Kruskal
Dijkstra & Prim
O(e loge  eG(n))
O(e  n2 )
O(e  n log n)
Theorem : Dijkstra/Prim Algorithm correctly
constructs a min-cost spanning tree.
[proof]
( By induction on # of edges chosen )
min-cost edge
(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.
Algorithm : Minimum spanning tree(Dijkstra/Prim)
1. { Initialization }
Let x be an arbitrary vertex ;
O(1)
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 do
3. { 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}
O(|E|)
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} ;
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.
O(n2)
{ x and e are no longer fringe and candidate. }
end { if }
end { while }
O(|E|+n2)
2
A
fringeLink
B
4
6
G
3 2 C
1
F 5
I 4 H
2
6
8
2
E 1 D
2
4
C
7
F
B
A
3
1
I
3
H
G
adjacencyList
fringeWgt
3
7
parent
A
B
C
D
E
F
G
H
I
status
intree
nil
F
2
4
A
B
intree
fringe
unseen
unseen
nil
F
C
H
7
3
3
1
A
A
G
G
fringe
intree
fringe
fringe
Shaded entries in fringeLink are
no longer in use. fringeList = I.
[ Minimum spanning tree data structure ]
(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)
2
3
12 e
4
14
10 15
G = (V, E)
w(f)  2
fE{e}
11
12
7
9
5
e must not be contained in a min-cost spanning tree.
why ?
Now, w(e) = 1
Contradiction ! why ?
 (|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
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 = min
{ vj }
jT
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.
1
1
3
3
2
2
4
2
s1 d4
w12 = 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}
1  
jT
 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} = 4
v4 = min{v4, v2+w24} = min{, 1+2} = 3
min{vj} = min{v3, v4} = min{4, 3} = v4
jT
k=4
T := {3,4}/{4} = {3}
P := P {4} = {1,2,4}
Is k=4 ? Yes !!!
Why does Dijkstra’s Algorithm Work ?
y'
x'
s
x
y
e
mi
n
4. Traversing Graphs and Digraphs
start
8 Queen Problem
Q
Recursion
· · · · · · ·
Q · · · · ·
Q · · ·
Q
· ·
Q
·
(1,1)
(2,3)
(3,5)
(1,2) (1,3)
(1,4) (1,5) (1,6) (1,7) (1,8)
(2,4)
(2,5) (2,6) (2,7) (2,8)
(3,6)
(3,7)
(3,8)
Depth First Search
(4,2)
(5,4)
(4,7)
(4,8)
(5,8) (5,2) (5,4) (5,3) (5,4)
(6,4)
(6,4)
(7,6)
(7,6)
A
C
1
1
C
B
1
1
2
2
A
A
B
C
1
2
1
C
1
2
1
Depth First Search
B
C
1
C
1
1
2
1
2
Breadth First Search
A
F
E
B
D
C
G
H
1
F
D
4
A
6
E
B 3
2
5
A
1
H
7
C
Depth First Search
F
2 B 3
G
C
5
D
6
G
4
7
E
H
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}
1
A
5
F
6
3
E
B 4
3
2
D
G
H
7
C
B
1 A
2
4
5
D
E
F
G
7
H
C
Breadth First Search
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}
6
Depth First Search
undirected graphs
Tree edges
Back edges
(no other types)
Depth First Search Tree ( Directed Graph )
A
G
F
I
B
2
B
C
H
G
E
A 7
1
C
3
H 5
4
I
6
E
Tree edges
Back edges
Cross edges
Descendant edges
(forward)
Note : (1) top
(2) left
down
right
why not in
undirected graph?
F
A Generalized Depth-first Search Skeleton
• How to process vertices
– first visited ①
– back up to a vertex ②
– back up from a vertex ③
①
③
v
②
• How to process edges
– tree edges
– back edges
– descendant edges
– cross edges
Depending on applications, we have to take proper action for each of cases.
Algorithm : General Depth-first Search Skeleton
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}
G=(V,E)
Adjaciency Lists
B
DSF Tree
1
A
C
A
A
2
C
1
B
B
3
A
C
2, 4
C
3
back edge
A
D
E
C
B
B
tree edge
4
D
E
D
C
E
C
D
E