Download presentation

Presentation is loading. Please wait.

Published byKellen Dunn Modified over 2 years ago

1
© DEEDS 2008Graph Algorithms1 Application: Process Relations Some algorithms require that sub processes (tasks) are executed in a certain order, because they use data from each other Example: get dressed In which order should I don my clothes? Such an order is known as a topological sort Tee-shirt before shirt Briefs before trousers Socks before shoes Pullover before coat Trousers before shoes Shirt before pullover Trousers before coat

2
© DEEDS 2008Graph Algorithms2 Application: Process Dependencies Are these the only rules? Tee-shirt Briefs PulloverShirt Trousers Shoes Coat Socks Tee-shirt before shirt Briefs before trousers Socks before shoes Pullover before coat Trousers before shoes Shirt before pullover Trousers before coat

3
© DEEDS 2008Graph Algorithms3 Application: Process Relations Transitivity Tee-shirt Briefs PulloverShirt Trousers Shoes Coat Socks Relation??

4
© DEEDS 2008Graph Algorithms4 Partial Order A partial order is a relation that (informally) expresses: For any two objects holds that either... The object relate with a “greater/smaller than”, “before/after” type relation ...or the two objects are incomparable (shirt or socks first?) So what is so partial about the ordering?

5
© DEEDS 2008Graph Algorithms5 Acyclic Graphs and Partial Order Definition: A partial Order (M, ) consists of a set M and a irreflexive, transitive, antisymmetric order relation () Cormen’s definition is reflexive, transitive, antisymmetric In a partial order, some elements can be incomparable Acyclic graphs specify partial orders : Let G = (V,E) be an acyclic, directed graph G + = (V,E + ) be the corresponding transitive closure The partial order ( V, G ) is defined as: v i G v j ( v i, v j ) E + p. 1075

6
© DEEDS 2008Graph Algorithms6 Transitive Closure There are unfortunately multiple definitions for the transitive closure… There is another definition, where G * is called the transitive closure (Cormen for instance) G* = (V,E*), where E* = E + { (v,v) | v V } G* sometimes also known as the reflexive transitive closure We will use: G + = transitive closure (Wegegraph in German) For all definitions: use the one on the slides! Also for the exam!

7
© DEEDS 2008Graph Algorithms7 Maths… Let be a relation (on for example ) Reflexive (=,≤,≥) Irreflexive (<) Antisymmetric (≤,≥) Transitive (=,<, ≤,≥)

8
© DEEDS 2008Graph Algorithms8 Example E + = { (v1, v3), (v1, v4), (v1, v5), (v1, v6), (v1, v7), (v2, v3), (v2, v4), (v2, v5), (v2, v6), (v2, v7), (v3, v4), (v3, v5), (v3, v6), (v3, v7), (v4, v6), (v5, v6), (v5, v7) } Graph G

9
© DEEDS 2008Graph Algorithms9 Deadlock Test: find cycles = test for partial order How do we do this? Example (fictive :-) Assume: “The coat should be put on before the shirt” Tee-shirt Briefs PulloverShirt Trousers Shoes Coat Socks

10
© DEEDS 2008Graph Algorithms10 Topological Sort Topological sort order: a sequence of objects that adhere to a partial order Example: Tee-shirt Shirt Pullover Briefs Trousers Socks Shoes Coat or Briefs Tee-shirt Trousers Shirt Socks Shoes Pullover Coat Tee-shirt Briefs PulloverShirt Trousers Shoes Coat Socks p. 549

11
© DEEDS 2008Graph Algorithms11 Topological Sort Definition: The arrangement of elements m i of a partially ordered set (M,) in a sequence m 1, m 2,..., m k is called topological sort if for any two indices i and j with 1 i < j k the following holds: Either m i m j or m i and m j are incomparable Topological Sort is a “linearization” of elements, that is compatible with partial order Visualized as a graph: Arrangement of vertices on a horizontal line so that all edges go from left to right v1v1 v3v3 v4v4 v2v2 v5v5 v6v6 v7v7 v1v1 v2v2 v3v3 v5v5 v4v4 v7v7 v6v6

12
© DEEDS 2008Graph Algorithms12 Finding of a Topological Sort Finding a topological sort can be done using graph algorithms Starting point: Determine the acyclic graph G that corresponds to (M,). Two possible approaches: (top down): As long as G is not empty... 1.Find a vertex v with incoming degree 0 2.Delete v from G together with its corresponding edges 3.Save the deleted vertices in this order (bottom up): 1.Starting from the vertex with incoming degree 0, start a DFS traversal. 2.Assign reverse “visit numbers” beginning with the “deepest” vertex.

13
© DEEDS 2008Graph Algorithms13 Example: top down 1.Find a vertex with d - (v) = 0 2.Delete from G vertex v and all incident edges 3.Save the deleted vertices in this order 4.Repeat steps 1 to 3 until G is empty Graph G Possible topological sorts: 1, 2, 3, 4, 5, 6, 72, 1, 3, 5, 4, 6, 71, 2, 3, 4, 5, 7, 6 2, 1, 3, 5, 7, 4, 6... 1 3 2 45 67

14
© DEEDS 2008Graph Algorithms14 Cost: top down In worst case you always find the next vertex with in- degree 0 last O(|V| 2 ) Example: v4v3v2v1

15
© DEEDS 2008Graph Algorithms15 Example: bottom up (DFS) Given: acyclic Graph G = ( V, E ) Find: topological sort TS[v] for all v V for ( all v V ) { v.unvisit( ); }/* initialization */ z = |V|; /* maximum number in TS */ for ( all v V ) { if ( d - (v) == 0 ) v.topSort( );/* all starting vertices */ } void topSort( ) {/* DFS Traversal */ this.visit( ); for ( all k‘ this.neighbors( ) ) if ( k‘.unvisited( ) ) k‘.topsort(); TS[ v ] = z; /* Assignment of TS at the end*/ z--; }

16
© DEEDS 2008Graph Algorithms16 Example Vertices are named “backwards” while ascending from DFS recursion. Sorting order: 2 1 3 5 7 4 6 7 6 3 5 4 12 1 3 2 45 67

17
© DEEDS 2008Graph Algorithms17 Cost: bottom up Consists of Search for nodes with in-degree 0 O (|V|) DFS on the corresponding subgraphs O (|V|+|E|) Total cost O(max (|V|,|E|) A bit more complicated, but “better” than top down

18
© DEEDS 2008Graph Algorithms18 Side kick: Total Order There is not only partial order Total order is a partial order where all elements in the set M are pair wise comparable For example: ‘≤’ and ‘≥’ But not: “is a child of” over all Germans Where could these orders be useful? Deadlock detection in concurrent programs Message ordering and consistency in distributed computing

19
© DEEDS 2008Graph Algorithms19 Application: Cable Routing The possible cable channels are defined by the channels Goal: every computer should be reachable

20
© DEEDS 2008Graph Algorithms20 Solution: Spanning Tree Model the problem as a graph Create a spanning tree As long there is a cycle left: Remove an edge from a cycle 1 3 2 4 5 67 8

21
© DEEDS 2008Graph Algorithms21 Spanning Trees Definition: A subgraph B is a spanning tree of a graph G, if there are no cycles in B and B contains all vertices in G It is not difficult to build a spanning tree of a connected graph: If G is acyclic, then G is a spanning tree itself. If G contains cycles, delete an edge from the cycle (the graph stays connected) and repeat the procedure until there are no more cycles. Ch. 23

22
© DEEDS 2008Graph Algorithms22 Spanning Trees (2) Edges in G and B are called branches Edges in G and not in B are called chords A graph with n vertices and e edges has n-1 branches and e - n + 1 chords The spanning tree of a graph is not unique All spanning trees of a graph can be systematically constructed by means of an acyclic exchange: Find any spanning tree B of G Add a chord to B (that leads to a cycle in B) Delete another edge from B

23
© DEEDS 2008Graph Algorithms23 Example B 1, B 2, B 3 - all spanning trees of G Chords (edges in G and not in B) Branches (edges in B) Graph G B1B1 B2B2 B3B3

24
© DEEDS 2008Graph Algorithms24 Application: Cable Routing The green numbers indicate the lengths of the cables How do we find the cheapest (shortest) routing?

25
© DEEDS 2008Graph Algorithms25 Minimum Spanning Trees We look at spanning trees of labeled graphs where edges are labeled with c. Definition: The weight c of a spanning tree B = (V,E) is the sum of the weights of its branches, i.e. c(B) = eE c(e). Definition: A spanning tree B = ( V, E ) is called the minimum spanning tree of graph G, if for any B‘ = ( V, E‘ ) of G the following holds: c(B) c(B‘)

26
© DEEDS 2008Graph Algorithms26 Example c(B 1 ) = 12, c(B 2 ) = 15, c(B 3 ) = 16 B 1 is minimum spanning tree of G Chord Branch labeled graph G 5 3 7 4 B1B1 B2B2 B3B3

27
© DEEDS 2008Graph Algorithms27 Finding Minimum Spanning Trees Idea: greedy-principle, i.e. decide on the basis of the knowledge currently available and never revise the decision. Efficient in contrast to methods that sample solutions, evaluate and revise them if necessary. Given: Graph G = ( V, E ) labeled with c Find: minimum spanning tree T‘ = (V, E‘) of G. E‘ = { }; while (not yet finished) { choose an appropriate edge e E; E‘ = E‘ { e } }

28
© DEEDS 2008Graph Algorithms28 Kruskal‘s Algorithm Sampling of edges according to the principle of the lowest weight. Sort edges according to their weights. Choose the edge with the lowest weight. If E‘ { e } results in a cycle, drop e. e Observation: If there is a cycle, it is pointless to leave e in the tree and to drop another edge e‘, because c(e‘) < c(e). e‘

29
© DEEDS 2008Graph Algorithms29 Example: Kruskal Sorting: (1, 2) (3, 4) (2, 3) (3, 6) (4, 6) (1, 5) (5, 6) (5, 7) (4, 8) (7, 8) (2, 7) (1, 7) 1 3 2 4 5 67 8 5 6 5 22 9 11 7 8 14 19 16 Needed length: 60m

30
© DEEDS 2008Graph Algorithms30 Kruskal‘s Algorithm: Complexity The complexity comes from Cost for sorting the edges (possible with O(|E| log |E|)) Cost for testing for cycles (also O(|E| log |E|)) Total: O(|E| log |E|)

31
© DEEDS 2008Graph Algorithms31 Prim‘s Algorithm Enhancement of Kruskal‘s Algorithm by Prim (sometimes Dijkstra is declared as the author): Given: G = ( V, E ) labeled with c Find: minimum spanning tree T = ( V, E‘ ) of G V‘ = { arbitrary start vertex v V } for (int i = 1; i |V| - 1; i++ ) { e = edge (u,v) E with u V‘ and v V‘ and minimum weight c V‘ = V‘ { v }; E‘ = E‘ { e } }

32
© DEEDS 2008Graph Algorithms32 Example 1 3 2 4 5 67 8 5 6 5 22 9 11 7 8 14 19 16

33
© DEEDS 2008Graph Algorithms33 Analysis (Efficient) cycle testing is an integral part of Kruskal‘s algorithm: Interconnecting two different connected components of a graph by an edge does not lead to a cycle The vertices belonging to the new edge must be located in different components Upon the assembly, the smaller component is added to the larger one Works with appropriate data structures in O( |E| log |V| ) time

34
© DEEDS 2008Graph Algorithms34 Path Finding Problems Finding certain paths in a graph Distinct boundary conditions lead to different problems: (un)directed graph Type of weights (only positive or also negative weights, bounded weights?) Shortest vs. longest path Path existence (Number of Paths) Start and end vertex: Paths between a specified pair of vertices Paths between a start vertex and all others Paths between every pair of vertices Ch. 24

35
© DEEDS 2008Graph Algorithms35 Application: Route Planning

36
© DEEDS 2008Graph Algorithms36 Abstraction: Model as a Graph

37
© DEEDS 2008Graph Algorithms37 Refinement

38
© DEEDS 2008Graph Algorithms38 1. Approach: Brute force Brute force: The simplest way Try all possible paths Cost: normally exponential Worst case: Complete graph n cities (n-1)! Different paths with (n-1) edges each complexity O(nn!)

39
© DEEDS 2008Graph Algorithms39 1. Approach: Brute force 5 Nodes: 96 paths 10 Nodes : 3.265.920 paths 100 Nodes : 923929532895047111548822464677040334858088085 817378052539070342562654239932976164528520493 363949531033911609416189515206686733588076953 60000000000000000000000 paths All 12903 communities in Germany:

40
© DEEDS 2008Graph Algorithms40

41
© DEEDS 2008Graph Algorithms41 2. Approach: Ants How do ants find a short path from I to O? Solution: search concurrently! Instead of testing, find all ways at once

42
© DEEDS 2008Graph Algorithms42 The Dijkstra Algorithm

43
© DEEDS 2008Graph Algorithms43 Shortest Path Definition: The weight of a path p = v 1, v 2,..., v k in a graph G = ( V, E ) with edges labeled with c, is the sum of the weights of its constituent edges, e.g.: c(p) = c(( v i, v i+1 )) Definition: The shortest path between two vertices u and v in G is the path with minimum weight The weight of the shortest path is called the distance between u and v In non-weighted graphs, c(p) = |p| k-1 i=1

44
© DEEDS 2008Graph Algorithms44 Example All paths from a to d: p 1 = a, b, c, d p 2 = a, b, d Shortest path from a to d: p 2 (weight 9) Shortest path from e to f: p 3 = e, f (weight 2) Shortest path from a to e? 5 3 7 4 ab cd e f 2

45
© DEEDS 2008Graph Algorithms45 Single Source Problem single source shortest path: Given a graph G = ( V, E ), we want to find a shortest path from a source vertex s V to all other vertices v V. BFS-based algorithm (by Moore): uses length attribute D of a vertex to keep the distance (as number of hops). for ( all v V ) { v.D = } /* Initialization */ Queue Q = new Queue( ); Q.enqueue( s ); s.D = 0; i = 0;/* s is source vertex */ while ( !Q.empty( ) ) { w = Q.dequeue( ); if ( w.D == i ) i++;/* next level in BFS */ for ( all v w.neighbours( ) ) { if ( v.D == ) { v.D = i; Q.enqueue(v); } }

46
© DEEDS 2008Graph Algorithms46 Example Visited vertexiv1v2v3v4v5v6q 00 v1 1111v2,v3,v6 v22v3,v6 v322v6,v4,v5 v6v4,v5 v43v5 - “Wave propagation” in the graph

47
© DEEDS 2008Graph Algorithms47 Analysis Moore‘s Algorithm has the same time complexity as BFS, i.e. O( max( |E|, |V| )). The algorithm does not work for weighted edges Enhancement by Dijkstra was published 1959!

48
© DEEDS 2008Graph Algorithms48 Dijkstra‘s Algorithm single source shortest path with arbitrary positive edge weights, start vertex s Correctness is based on the principle of optimality: For any shortest path p = v 1,..., v k from v 1 to v k, each subpath p‘ = v i,..., v j with 1 i < j k is a shortest path between v i and v j. Proof: Assume that this does not hold, i.e. there is a shorter path p‘‘ p‘ between v i and v j. Then, there exists a shorter path between v 1 and v k by exchanging subpath p‘ with p“.

49
© DEEDS 2008Graph Algorithms49 Dijkstra‘s Algorithm (2) Each vertex belongs to one of the categories M 1, M 2, M 3. Extract vertex v from M 2 to which there is a minimal edge between M 1 und M 2 and insert v in M 1 (principle of optimality). For each vertex that was moved to M 1, move its successors to M 2. s u v not reached vertices marginal vertices selected vertices M1M1 M2M2 M3M3

50
© DEEDS 2008Graph Algorithms50 Dijkstra‘s Algorithm (3) More precisely: Divide the vertices in the graph into three subsets: M1 contains all vertices v, for which shortest paths between s and v are already known. M2 contains all vertices v, that are reachable via an edge from a vertex in M1. M3 contains all other vertices. Invariant: denote sp(x,y) as the shortest path from x to y. Then, the following holds: For all shortest paths sp(s,u) and all edges (u,v) c(sp(s,u)) + c((u,v)) c(sp(s,v)) For at least one shortest path sp(s,u) and one edge (u,v) c(sp(s,u)) + c((u,v)) = c(sp(s,v))

51
© DEEDS 2008Graph Algorithms51 Dijkstra‘s Algorithm /* M 3 implicitly defined as V \ ( M 1 M 2 ) */ for (all v V) { if ( v s ) v.L = ; } /* Initialization */ M 1 = { s }; M 2 = { }; s.L = 0; for (all v s.neighbors( ) ) {/* fill M 2 initially */ M 2 = M 2 { v }; v.L = c((s,v)); } while ( M 2 { } ) { v = the vertex in M 2 with minimum v.L; M 2 = M 2 \ { v }; M 1 = M 1 { v };/* move v */ for (all w v.neighbours( ) ) { if ( w M 3 ) { /* move neighbours and update L */ M 2 = M 2 { w }; w.L = v.L + c((v,w)); } else if ( w.L > v.L + c((v,w)) ) /* w in M 2 (or M 1 )*/ w.L = v.L + c((v,w)); }

52
© DEEDS 2008Graph Algorithms52 (7) Example vM1M2v1v2v3v4v5v6 {{ }0 v1v2,v3,v6326 v3 v2,v4,v5,v667 v2 v4,v5,v64 v6 v4,v55 v4 V5 v5 } 1 2 36 5 4 3 3 2 6 1 5 5 2 1 4 3 (6) (2) (3) (6) (4) (5)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google