1 / 10

Algorithms ( and Datastructures )

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

heidi
Download Presentation

Algorithms ( and Datastructures )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms (andDatastructures) Lecture 10 MAS 714 part 2 Hartmut Klauck

  2. 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

  3. 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)

  4. 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

  5. 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

  6. 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

  7. 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.

  8. 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

  9. 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 us,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

  10. Motivation • The Max Flow problemmodelsthesituationwherecommoditiesneedtobetransportedthrough a networkwith limited capacities • Applicationtootherproblems

More Related