informatik ii
play

Informatik II Ubung 11 FS 2019 1 Program Today Last Week: BFS - PowerPoint PPT Presentation

Informatik II Ubung 11 FS 2019 1 Program Today Last Week: BFS with Lazy Deletion 1 Adjacency List in Java, continued 2 Repetition of Lecture 3 Dijkstras Algorithm Bellman-Ford Algorithm In-Class-Exercise (theoretical) 4


  1. Informatik II ¨ Ubung 11 FS 2019 1

  2. Program Today Last Week: BFS with Lazy Deletion 1 Adjacency List in Java, continued 2 Repetition of Lecture 3 Dijkstra’s Algorithm Bellman-Ford Algorithm In-Class-Exercise (theoretical) 4 In-Class-Exercise (practical) 5 2

  3. BFS with Lazy Deletion public void BFS2(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<Integer>(); queue.add(s); while (!queue.isEmpty()) { int u = queue.poll(); if (!visited[u]) { visited[u] = true; System.out.print(u + " "); for (int v : adj.get(u)) queue.add(v); } } }

  4. BFS with Lazy Deletion public void BFS2(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<Integer>(); queue.add(s); while (!queue.isEmpty()) { int u = queue.poll(); if (!visited[u]) { visited[u] = true; System.out.print(u + " "); for (int v : adj.get(u)) queue.add(v); } A node is pushed on } Queue once for each in- } coming edge.

  5. BFS with Lazy Deletion public void BFS2(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<Integer>(); queue.add(s); Node marked as visited, while (!queue.isEmpty()) { but its copies are not int u = queue.poll(); immediately removed from Queue. if (!visited[u]) { (“Lazy Deletion”) visited[u] = true; System.out.print(u + " "); for (int v : adj.get(u)) queue.add(v); } A node is pushed on } Queue once for each in- } coming edge. 3

  6. Adjacency List Unweighted Graph class Graph { // G = (V,E) as adjacency list private int V; // number of vertices private ArrayList<LinkedList<Integer>> adj; // adj. list // Constructor public Graph(int n) { V = n; adj = new ArrayList<LinkedList<Integer>>(V); for (int i=0; i<V; ++i) adj.add(i,new LinkedList<Integer>()); } // Edge adder method public void addEdge(int u, int v) { adj.get(u).add(v); } } 4

  7. Adjacency List weighted Graph class Graph { // G = (V,E) as adjacency list private int V; // number of vertices private ArrayList<LinkedList<Pair>> adj; // adj. list // Constructor public Graph(int n) { V = n; adj = new ArrayList<LinkedList<Pair>>(V); for (int i=0; i<V; ++i) adj.add(i,new LinkedList<Pair>()); } // Edge adder method, (u,v) has weight w public void addEdge(int u, int v, int w) { adj.get(u).add(new Pair(v,w)); } } 5

  8. Adjacency List weighted Graph public class Pair implements Comparable<Pair> { public int key; public int value; // Constructor public Pair(int key, int value) { this.key = key; this.value = value; } @Override // we need this later... public int compareTo(Pair other) { return this.value − other.value; } // for general usage of pairs we would also need // to provide equals(), hashCode(), ... } 6

  9. 3. Repetition of Lecture 7

  10. Weighted Graphs Given: G = ( V, E, c ) , c : E → ❘ , s, t ∈ V . Wanted: Length (weight) of a shortest path from s to t . Path: p = � s = v 0 , v 1 , . . . , v k = t � , ( v i , v i +1 ) ∈ E ( 0 ≤ i < k ) Weight: c ( p ) := � k − 1 i =0 c (( v i , v i +1 )) . 2 3 1 2 1 t S Path with weight 9 8

  11. Shortest Paths Weight of a shortest path from u to v : � ∞ no path from u to v δ ( u, v ) = p min { c ( p ) : u � v } sonst 9

  12. Ingredients of an Algorithm Wanted: shortest paths from a starting node s . Weight of the shortest path found so far d s : V → ❘ At the beginning: d s [ v ] = ∞ for all v ∈ V . Goal: d s [ v ] = δ ( s, v ) for all v ∈ V . Predecessor of a node π s : V → V Initially π s [ v ] undefined for each node v ∈ V 10

  13. General Algorithm 1 Initialise d s and π s : d s [ v ] = ∞ , π s [ v ] = null for each v ∈ V 2 Set d s [ s ] ← 0 3 Choose an edge ( u, v ) ∈ E Relaxiere ( u, v ) : if d s [ v ] > d [ u ] + c ( u, v ) then d s [ v ] ← d s [ u ] + c ( u, v ) π s [ v ] ← u 4 Repeat 3 until nothing can be relaxed any more. (until d s [ v ] ≤ d s [ u ] + c ( u, v ) ∀ ( u, v ) ∈ E ) 11

  14. Assumption a c 6 2 1 s e 2 3 3 1 1 b d All weights of G are positive . 12

  15. Basic Idea Set V of nodes is partitioned into the set M of nodes for which a 3 shortest path from s is already known, 2 v ∈ M N + ( v ) \ M of the set R = � 2 5 1 nodes where a shortest path is not yet s 5 2 known but that are accessible directly from M , 2 the set U = V \ ( M ∪ R ) of nodes that have not yet been considered. 13

  16. Induction Induction over | M | : choose nodes from R with smallest upper bound. Add r to M 3 and update R and U accordingly. 2 2 5 1 s Correctness: if within the “wavefront” a 5 2 node with minimal weight w has been 2 found then no path over later nodes (pro- viding weight ≥ d ) can provide any im- provement. 14

  17. Algorithm Dijkstra( G, s ) Input: Positively weighted Graph G = ( V, E, c ) , starting point s ∈ V , Output: Minimal weights d of the shortest paths and corresponding predecessor node for each node. foreach u ∈ V do d s [ u ] ← ∞ ; π s [ u ] ← null d s [ s ] ← 0 ; R ← { s } while R � = ∅ do u ← ExtractMin( R ) foreach v ∈ N + ( u ) do if d s [ u ] + c ( u, v ) < d s [ v ] then d s [ v ] ← d s [ u ] + c ( u, v ) π s [ v ] ← u R ← R ∪ { v } 15

  18. Example a c 6 2 1 s e 2 3 3 1 1 b d 16

  19. Example ∞ ∞ a c 6 2 1 M = { s } 0 R = {} ∞ s s e 3 2 U = { a, b, c, d, e } 3 1 1 ∞ ∞ b d 16

  20. Example 2 ∞ ∞ a a c 6 2 1 M = { s } R = { a, b } 0 ∞ s s e 2 3 U = { c, d, e } 3 1 3 1 ∞ ∞ b b d 16

  21. Example 8 2 ∞ ∞ a a a c c 6 2 1 M = { s, a } R = { b, c } 0 ∞ s s e 2 3 U = { d, e } 3 1 3 1 ∞ ∞ b b d 16

  22. Example 8 2 ∞ ∞ a a a c c 6 2 1 M = { s, a, b } R = { c, d } 0 ∞ s s e 2 3 U = { e } 3 1 3 4 1 ∞ ∞ b b b d d 16

  23. Example 8 2 7 ∞ ∞ a a a c c 6 2 1 M = { s, a, b, d } 0 5 ∞ R = { c, e } s s e e 2 3 U = {} 3 1 3 4 1 ∞ ∞ b b b d d d 16

  24. Example 6 8 2 7 ∞ ∞ a a a c c 6 2 1 M = { s, a, b, d, e } 0 5 ∞ R = { c } s s e e e 2 3 U = {} 3 1 3 4 1 ∞ ∞ b b b d d d 16

  25. Example 6 8 2 7 ∞ ∞ a a a c c c 6 2 1 M = { s, a, b, d, e, c } 0 5 ∞ R = {} s s e e e 2 3 U = {} 3 1 3 4 1 ∞ ∞ b b b d d d 16

  26. Implementation: Data Structure for R ? Required operations: Insert (add to R ) ExtractMin (over R ) and DecreaseKey (Update in R ) foreach v ∈ N + ( u ) do if d s [ u ] + c ( u, v ) < d s [ v ] then d s [ v ] ← d s [ u ] + c ( u, v ) π s [ v ] ← u if v ∈ R then DecreaseKey( R, v ) // Update of a d ( v ) in the heap of R else R ← R ∪ { v } // Update of d ( v ) in the heap of R MinHeap! 17

  27. DecreaseKey DecreaseKey: climbing in MinHeap in O (log | V | ) Position in the heap (i.e. array index of element in the heap)? 18

  28. DecreaseKey DecreaseKey: climbing in MinHeap in O (log | V | ) Position in the heap (i.e. array index of element in the heap)? alternative (a): Store position at the nodes 18

  29. DecreaseKey DecreaseKey: climbing in MinHeap in O (log | V | ) Position in the heap (i.e. array index of element in the heap)? alternative (a): Store position at the nodes alternative (b): Hashtable of the nodes 18

  30. DecreaseKey DecreaseKey: climbing in MinHeap in O (log | V | ) Position in the heap (i.e. array index of element in the heap)? alternative (a): Store position at the nodes alternative (b): Hashtable of the nodes alternative (c): re-insert node each time after update-operation and mark it as visited ("deleted") once extracted (Lazy Deletion) 18

  31. Runtime | V |× ExtractMin: O ( | V | log | V | ) | E |× Insert or DecreaseKey: O ( | E | log | V | ) 1 × Init: O ( | V | ) Overal: O ( | E | log | V | ) . 19

  32. General Weighted Graphs Relaxing Step as with Dijkstra: Relax( u, v ) d s ( u ) if d s ( v ) > d s ( u ) + c ( u, v ) then u d s ( v ) ← d s ( u ) + c ( u, v ) π s ( v ) ← u d s ( v ) return true s v return false Problem: cycles with negative weights can shorten the path, a shortest path is not guaranteed to exist. 20

  33. Observations Observation 1: Sub-paths of shortest paths are shortest paths. Let p = � v 0 , . . . , v k � be a shortest path from v 0 to v k . Then each of the sub-paths p ij = � v i , . . . , v j � ( 0 ≤ i < j ≤ k ) is a shortest path from v i to v j . Proof: if not, then one of the sub-paths could be shortened which immediately leads to a contradiction. Observation: If there is a shortest path then it is simple, thus does not provide a node more than once. Immediate Consequence of observation 1. 21

  34. Dynamic Programming Approach (Bellman) Induction over number of edges d s [ i, v ] : Shortest path from s to v via maximally i edges. d s [ i, v ] = min { d s [ i − 1 , v ] , min ( u,v ) ∈ E ( d s [ i − 1 , u ] + c ( u, v )) d s [0 , s ] = 0 , d s [0 , v ] = ∞ ∀ v � = s. 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend