Graph Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Algorithms

Description:

A cut (X, Y) of a graph G = (V, E) is a partition of the vertex set V into two ... ??? cut(x,y)- ????? ?? ???????? ?- 2 ?????? ???? ????????. ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 30
Provided by: dan48
Category:

less

Transcript and Presenter's Notes

Title: Graph Algorithms


1
Graph Algorithms
3/7/2007
???? ?????? ?? ???? ??????. ?? ?????? ???? ??
??????? ???????, ??? ????? ?? ?? ????
Minimum Spanning Trees (MST) Union - Find Dana
Shapira
2
Spanning tree
?? ????- ???? ?? ?? ????????? ?? ????, ??? ??
????? ?? ?? ??????? (??? ?? ????? ??????? ????).
A spanning tree of G is a subset T ? E of edges,
such that the sub-graph G'(V,T) is connected and
acyclic.
3
Minimum Spanning Tree
?? ???? ???????- ??? ?? ???? ????? ???????? ??
?????? ??? ??? ??????? (MST)
Given a graph G (V, E) and an assignment of
weights w(e) to the edges of G, a minimum
spanning tree T of G is a spanning tree with
minimum total edge weight
1
3
3
6
6
9
1
5
7
8
3
2
7
4
4
How To Build A Minimum Spanning Tree
??? ????? ?? ???? ???????, ????? ?????? ???? ????
???? ???.
  • General strategy
  • Maintain a set of edges A such that (V, A) is a
    spanning forest of G and such that there exists a
    MST (V, F) of G such that A?F.
  • As long as (V, A) is not a tree, find an edge
    that can be added to A while maintaining the
    above property.
  • Generic-MST(G(V,E))
  • A ?
  • while (A is not a spanning tree of G) do
  • choose a safe edge e(u,v)?E
  • AA?e
  • return A

???? ?-A ?? ????? ?????? ?? G, ???? ????? ???' ??
?????? ?? ??? ????? ????????. ????? ?? ?? ????
????? ?? ????? ??? ????? ????????.
????? ?????? ????? ??????- ??? ?????? ??? ?????
???????? ????? ???? ???? ?-A ???? ???? ??? ???
???- ??? ?? ???? ??????? ???? ????? ????????
????? ????? ????? ?????
5
Cuts
??? cut(x,y)- ????? ?? ???????? ?- 2 ?????? ????
????????. ??? ???? ?? ???? ?? ?????? ??? ????
?????? ??? ???????? ???? ?????? ?????? (????? ???
?????? ???????). ????? A ????? ?? ???? ?? ??? ??
????? ??????
A cut (X, Y) of a graph G (V, E) is a partition
of the vertex set V into two sets X and Y V \
X. An edge (v, w) is said to cross the cut (X, Y)
if v ? X and w ?Y.
A cut (X, Y) respects a set A of edges if no edge
in A crosses the cut.
6
A Cut Theorem
????? ?? ????? ?? ??? ?????, ?? A (A ????? ??
????). ????? ?-A ????? ????? ?? ?? ?????? ??????
?? ???? ?????? ???? ?? ???? ?? ????? ????????
????? ????? ?-A (???? ???? ??????). ???? ?? ?-A
????? ????? ?? ????? ?? ??? ?????. ?????? ???.
A Cut Theorem
Theorem Let A be a subset of the edges of some
minimum spanning tree of G let (X, Y) be a cut
that respects A and let e be a minimum weight
edge that crosses (X, Y). Then A ?e is also a
subset of the edges of a minimum spanning tree of
G edge e is safe.
????? ???????????-(????? ??????? ????? ?????) A
??? ?????? ?? ?????? ??????? ????? ????? ?? ???
????? ?? ???? ?????. ????? ?? ?? ?????? ???????
?????? ?? ????. ????? ???? ?? ???? ?? ????? ?????
????? ?????? ???? ?-A. ????? ?? ???? ??????
?????? ?????? ?? ?????? ??????? ?????? ??? ????
?? ?? ????. ???? ??? ?? ?????? ???? ??? ????
????? ????????. ????? ?? A ??? G. ?? ????
????????? ?????? e ????? ??? ?????, ?? ?????? ??
??????. ????, ?? ????? ??? P ????? ???? ??? ??
???? ?????????, ??????? ??? f. ???? f ???? ?-A
???? ?-A ????? ?? ????. ???? ?? ???? ??? ?? ?????
?? f ?????? ?? e ?????? ?? ?? ??? ???- T ?????
??? ????? T. T ???? ??? ????? ????????.
1
9
4
2
3
4
7
A Cut Theorem
Theorem Let A be a subset of the edges of some
minimum spanning tree of G let (X, Y) be a cut
that respects A and let e be a minimum weight
edge that crosses (X, Y). Then A ?e is also a
subset of the edges of a minimum spanning tree of
G edge e is safe.
12
9
4
8
A Cut Theorem
u
e
e
v
f
T
w(e) w(f)
w(e) w(f) w(T') w(T)
9
Proof
  • Let T be a MST such that A?T.
  • If e (u,v) ? T, add e to T.
  • The edge e (u,v) forms a cycle with edges on
    the path p from u to v in T. Since u and v are on
    opposite sides of the cut, there is at least one
    edge f (x,y) in T on the path p that also
    crosses the cut.
  • f ?A since the cut respects A. Since f is on the
    unique path from u to v in T, removing it breaks
    T into two components.
  • w(e) w(f) (why?)
  • Let T ' T f ?e ?w(T ') w(T).

10
A Cut Theorem
Corollary Let G(V,E) be a connected undirected
graph and A a subset of E included in a minimum
spanning tree T for G, and let C(VC,EC) be a
tree in the forest GA(V,A). If e is a light edge
connecting C to some other component in GA, then
e is safe for A.
  • Proof The cut (VC, VVC) respects A, and e is a
    light edge for this cut. Therefore, e is safe.

11
Kruskals Algorithm
Kruskal(G) 1 A ? Ø 2 for every edge e (v, w) of
G, sorted by weight 3 do if v and w belong to
different connected components of (V, A) 4 then
add edge e to A
??? ?????? ?? ???? ????? ?? A?
????? ?? ???? ???? ????? ?- A
?????? C. ???? ??? ??
??? ????? ??? ????? ?????? ??? ?? A ??? ????? ??
A. ?????? ???? ??????? e.
???? ????? ?-A ????? ??
????. ??? ??? ?????? ??? ????? ?????? ?? C ????
??? ??????, ???? ??? ????? ?? A. e
??? ??? ?? ????. ????? ???? ?? A. ??? e ??? ?????
????? ?????? ???? ?-A ?? ?-A ????? ????? ?? ?????
??????- ???? ??? ????? ????????. ???? ?????? ??
????? ?????. ???? ??????
?????? ?? ???? ???? ????? (?? A??' ????
??????) ????????? ??? ???? ?? ?????? (???? ??
?????? ??? ???? ?????? ???? ????)
a
a
9
9
1
1
b
b
d
d
3
3
4
4
c
c
5
5
e
e
1
1
12
12
8
3
8
3
6
6
f
f
5
5
i
i
2
2
g
g
(a, d)1 (h, i)1 (c, e)1 (f, h)2 (g, h)2 (b,
c)3 (b, f)3 (b, e)4 (c, d)5 (f, g)5 (e,
i)6 (d, g)8 (a, b)9 (c, f)12
2
2
1
1
h
h
12
Correctness Proof
Sorted edge sequence e1, e2, e3, e4, e5, e6, ,
ei, ei 1, ei 2, ei 3, , en
Every edge ej that cross the cut have a weight
w(ej) w(ei).
Hence, edge ei is safe.
13
Union-Find Data Structures
??? ???? ?? 2 ???????? ?????? ????? ???? ??????,
???? ????? ??????? union-find
  • Given a set S of n elements, maintain a partition
    of S into subsets S1, S2, , Sk
  • Support the following operations
  • Union(x, y) Replace sets Si and Sj such that x
    ? Si and y ? Sj with Si ? Sj in the current
    partition
  • Find(x) Returns a member r(Si) of the set Si
    that contains x
  • In particular, Find(x) and Find(y) return the
    same element if and only if x and y belong to the
    same set.
  • It is possible to create a data structure that
    supports the above operations in O(a(n))
    amortized time, where a is the inverse Ackermann
    function.

?????? ???? ???? ????? ??? a(n) ????? ??????? 5
14
Kruskals Algorithm Using Union-Find Data
Structure
Kruskal(G,w) A ? ? for each vertex v?V
do Make-Set(v) sort the edges in E in
non-decreasing weight order w for each edge
(u,v)?E do if Find-Set(u) ? Find-Set(v)
then A ? A ? (u,v)
Union(u,v) return A
15
Kruskals Algorithm Using Union-Find Data
Structure
????? ???? ?????? ???? ?? ?????????, ??? ???
????? ????
  • Analysis
  • O(E log E) time for everything except the
    operations on S
  • Cost of operations on S
  • O(a(E,V)) amortized time per operation on S
  • V 1 Union operations
  • E Find operations
  • Total O((V E)a(E,V)) running time
  • Total running time O(E lg E).

??? ??????? ????? ?????
??"? ???? ???? ?????? ?? ????
???? ?- a ?????? ???? ??? ???? ???? ????"?
16
Union/Find
????? ?? ??????? ?? ???? ??????? ????? ?? ???
????? ????
  • Assumptions
  • The Sets are disjoint.
  • Each set is identified by a representative of the
    set.
  • Initial state
  • A union/find structure begins with n elements,
    each considered to be a one element set.
  • Functions
  • Make-Set(x) Creates a new set with element x in
    it.
  • Union(x,y) Make one set out of the sets
    containing x and y.
  • Find-Set(x) Returns a pointer to the
    representative of the set containing x.

???? ???????
??????? ???? ?? ??????
17
Basic Notation
  • The elements in the structure will be numbered 0
    to n-1
  • Each set will be referred to by the number of one
    of the element it contains
  • Initially we have sets S0,S1,,Sn-1
  • If we were to call Union(S2,S4), these sets would
    be removed from the list, and the new set would
    now be called either S2 or S4
  • Notations
  • n Make-Set operations
  • m total operations
  • n?m

????? ?? ???????
????
????? ??????? ?? ?????? ??? ???? ????? ??? ?????
???? ?? ?????? ?????? ?????
18
First Attempt
????? ????? ?????? ??????? ???? ????? ???
  • Represent the Union/Find structure as an array
    arr of n elements
  • arri contains the set number of element i
  • Initially, arrii (Make-Set(i))
  • Find-Set(i) just returns the value of arri
  • To perform Union(Si,Sj)
  • For every k such that arrkj, set arrki

???? ????? ????? ?? n ???????
?????? ?? ???? ???? ???? ????, ???? arri ????
?? ????? ????. ?????? ?? ???? ???? ?????? ??????
?? ????. ?? ??? ???? ?-Si ??? ???? ?- arrj.
????? ???- O(1)
Union- O(n) ???? ??????? ?? ?? ?????
19
Analysis
?? ???? ??? ????? ???? ???
  • The worst-case analysis
  • Find(i) takes O(1) time
  • Union(Si,Sj) takes ?(n) time
  • A sequence of n Unions will take ?(n2) time

?????? ?? ?? ????? ?????
?????? ???? ??? ???? ???????? ????????
20
Second Attempt
????? ???, ???? ??? ?????? ????? ????? O(n).
  • Represent the Union/Find structure using linked
    lists.
  • Each element points to another element of the
    set.
  • The representative is the first element of the
    set.
  • Each element points to the representative.
  • How do we perform Union(Si,Sj)?

???? ?????? ??????. ?? ???? ????? ??? ???? ???
????? ?????? ??????, ???? ???? ?????? ??
??????. ???? ??? ????? ????? ?????? ?????? ??????
?????? ??????. ??? ???? union, ????? ????? ???
????? ?? ?????. ????? ??? ??????? ?????? ?????
?????? ????? ?????? (?????? ?? ??????)
????. ???? ???? ?????? ??? ???????, ??? ????
???? ????? find ???? ?? O(1). Find ????? ??????
?????, ????? ??? ??? ?????, ?????? ????, ??????
????? ???? ?????? (????? ??????).
21
Analysis
?? ???? ??? ????? ???? ???
  • The worst-case analysis
  • Find(i) takes O(1) time
  • Make-Set(i) takes O(1) time
  • Union(Si,Sj) takes ?(n) time (Why?)
  • A sequence of n Unions-Find will take ?(n2) time
    (Example?)

?? ???? ????? ?????? ?? n/2 ??????
????? n ??????. Make-set ????? O(n) ??????
??????? ??"? T(n2) ?? union-find. ???? ?? ??????
?????? ?? ???? union(x2,x1)

union(x3,x2) n-1
?????? union(xn,xn-1) ??
? ????? ????????
nO(1) (n-1) T(n) ( O(n) O(n2) ) / 2
T(n2)
???? ???' ???????
union
Make-set
22
Up-Trees
???? ?????? ???? ????? ????? ??? ?? ????? ??
????? ??? ???? ??? ??? ?? ??? ?? ???? ??? ?????
?? ??????
  • A simple data structure for implementing disjoint
    sets is the up-tree.
  • We visualize each element as a node
  • A set will be visualized as a directed tree
  • Arrows will point from child to parent
  • The set will be referred to by its root

H
X
F
A
W
B
R
H, A and W belong to the same set. H is the
representative
X, B, R and F are in the same set. X is the
representative
23
Operations in Up-Trees
???? ?? ??????? ?? ???
  • Follow pointer to representative element.
  • find(x)
  • if (x?p(x)) // not the representative
  • then p(x)?find(p(x))
  • return p(x)

P(x)- ??? ?? x
24
Union
  • Union is more complicated.
  • Make one representative element point to the
    other, but which way?Does it matter?

25
Union(H, X)
?????
???? ?????? ?????? ????? ?????? ???? ???? ??
?????? ??????. ?? ????? ???? ????? ?? find.
H
X
F
X points to H B, R and F are now deeper
A
W
B
R
H
X
F
H points to X A and W are now deeper
A
W
B
R
26
A worst case for Union
  • Union can be done in O(1), but may cause find to
    become O(n)

A
B
C
D
E
Consider the result of the following sequence of
operations Union (A, B) Union (C, A) Union
(D, C) Union (E, D)
???? ?? ????? ??? ?? ??? ??? ???? ??? ?????
E
D
C
B
A
27
Array Representation of Up-tree
???? ????? ?? ?? ???? ??????? ????
  • Assume each element is associated with an integer
    i0n-1. From now on, we deal only with i.
  • Create an integer array, An
  • An array entry is the elements parent
  • A -1 entry signifies that element i is the
    representative element.

?? ???? ???? ????? ???. ????? ?? ????? ???? -1.
28
Array Representation of Up-tree
??? ??????? ??????? ?????? ????
  • Now the union algorithm might be
  • Union(x,y)
  • Ay x // attaches y to x
  • The find algorithm would be
  • find(x)
  • if (Ax lt 0)
  • return(x)
  • else
  • return(find(Ax))
  • Performance ???

????? ?????
???? ?? ?? ????? ????? ???? ????? (??? ????
??????? ?? ????)
????? ?????? ?? ?????
29
Analysis
?? ???? ??? ????? ???? ???
  • Worst case
  • Union(Si,Sj) take O(1) time
  • Find(i) takes O(n) time
  • Can we do better in an amortized analysis?
  • What is the maximum amount of time n operations
    could take us?
  • Suppose we perform n/2 unions followed by n/2
    finds
  • The n/2 unions could give us one tree of height
    n/2-1
  • Thus the total time would be n/2 (n/2)(n/2)
    O(n2)
  • This strategy doesnt really help

???? ?? ?????? ?? ????? ??? ???? ???
Write a Comment
User Comments (0)
About PowerShow.com