100 likes | 203 Views
Algorithms ( and Datastructures ). Lecture 10 MAS 714 part 2 Hartmut Klauck. Prim. In Prim‘s algorithm edges in A always form a tree Use a priority queue as in Dijkstra : Operations: Init: initialize empty priority queue Insert(v,k): insert v with key k
E N D
Algorithms (andDatastructures) Lecture 10 MAS 714 part 2 Hartmut Klauck
Prim • In Prim‘s algorithm edges in A always form a tree • Use a priority queue as in Dijkstra: • Operations: • Init: initialize empty priority queue • Insert(v,k): insert v with key k • Build Heap: Make Heap with n elements • ExtractMin: Find and remove element with min. key • DecreaseKey(v,k): reduce the key of v to k • Implementation with Heap: • Build Heap: O(n) for n elements inserted • Extract Min: O(log n) per Operation • DecreaseKey: O(log n) per Operation
Prim • Inputs: graph G, weights W, root r • Output: minimum spanning tree as set A={(v, ¼(v))} of predecessor pointers • for all v2 V set key(v)=1 and (v)=NIL • key(r)=0, S=; • Init: BuildPriorityQueue Q of all (v,key(v)) • While Q not empty: • u=ExtractMin, S=S[ {u} • For all neighbors v of u: • If v not in S and key(v) > W(u,v) then(v)=u and key(v):=W(u,v)
Running Time Prim • Initialize, n times ExtractMin and m times DecreaseKey • In total O( (n+m) log n) time using Heaps • Implementing the priority queue such that m DecreaseKey operations take time O(m) [amortized analysis]: • Time O(n log n + m) • Linear time for m¸ n log n
Correctness • A is always a tree • For all v in Q the value key(v) is the weight of a lightest edge from v into S (if key(v)<1) • True in the beginning • When u removed from Q then key(u) is weight of a lightest edge by induction hypothesis • S with edges in A is a tree, u is not in S, so still a tree • Update of neighbors v of u: • key(v)<1 then key(v) is weight of a lightest edge {w,v} to S-{u}, if edge (u,v) is better: update, now key(v) still weight of a lightest edge
Traversing graphs • Euler tours • For an undirected graph G=(V,E) an Euler tour is a path that traverses every edge exactly once and ends at the same vertex as it starts • Same definition for directed graphs • Graph is Eulerian if it has an Euler tour
Euler tours • Theorem: an undirected graph is Eulerian, iff all vertices have even degree and all vertices of nonzero degree are in the same connected component. • Proof: Clearly the condition is necessary.To see that it is sufficient we will give an algorithm that will find an Euler tour in linear time.
Finding an Euler tour • Start at some vertex v1, follow any edge {vi,vi+1} until v1 is reached again • On the way mark edges as used and vertices as visited • At this time some edges may be unused • Find any vertex on the cycle with unused edges and start a tour from it until a cycle is formed, join the cycles • Continue until no vertex on the cycle has any unused edges • By assumption there are no unvisited vertices with degree>0 • Why don’t we get stuck? • Every vertex has even degree
Network Flows • A flow network is a directed graph G=(V,E) with nonegative edge weights C(u,v), called capacities • There is a source s and a sink t • We assume that for all v there is a path from s to v and from v to t • A flow in G is a mapping f from V£V to the reals: • For all u,v: f(u,v)· C(u,v) [capacity constraint] • For all u,v: f(u,v)=-f(v,u) [skew symmetry] • For all us,t: v f(u,v)=0 [flow conservation] • The value of a flow f is |f|=v f(s,v) • The Max Flow problem consists of finding the maximum value of any flow for G,C,s,t
Motivation • The Max Flow problemmodelsthesituationwherecommoditiesneedtobetransportedthrough a networkwith limited capacities • Applicationtootherproblems