data structures in java
play

Data Structures in Java Lecture 18: Spanning Trees 11/23/2015 - PowerPoint PPT Presentation

Data Structures in Java Lecture 18: Spanning Trees 11/23/2015 Daniel Bauer 1 A General View of Graph Search Goals: Explore the graph systematically starting at s to Find a vertex t / Find a path from s to t . Find the shortest


  1. Data Structures in Java Lecture 18: Spanning Trees 11/23/2015 Daniel Bauer 1

  2. A General View of Graph Search Goals: • Explore the graph systematically starting at s to • Find a vertex t / Find a path from s to t . • Find the shortest path from s to all vertices. • … v 1 v 2 v 3 v 4 v 5 v 6 v 7 2

  3. A General View of Graph Search In every step of the search we maintain • The part of the graph already explored. • The part of the graph not yet explored. • A data structure (an agenda) of next edges 
 (adjacent to the explored graph). v 1 v 2 v 3 v 4 v 5 v 6 v 7 Agenda: (v2,v5), (v4,v5), (v4,v7) 3

  4. A General View of Graph Search The graph search algorithms discussed so far differ almost only in the type of agenda they use: • DFS: uses a stack. • BFS: uses a queue. • Dijkstra’s: uses a priority queue. • Topological Sort: BFS with constraint on items in the queue. v 1 v 2 v 3 v 4 v 5 v 6 v 7 Agenda: (v2,v5), (v4,v5), (v4,v7) 4

  5. Correctness of Dijkstra’s Algorithm • We want to show that Dijkstra’s algorithm really finds the minimum path costs (we don’t miss any shorter solutions 
 by choosing the shortest edge greedily). • Proof by induction on the set S of visited nodes. • Base case: 
 |S|=1. Trivial. Length shortest path is 0. s 5

  6. Correctness of Dijkstra’s Inductive Step • Assume the algorithm produces the minimal path cost from s for the subset S, |S| = k. • Dijkstra’s algorithm selects the next edge (u,v) leaving S. • Assume there was a shorter path 
 from s to v that does not contain (u,v). • Then that path must contain x another edge (x,y) leaving S. • The cost of (x,y) is already higher than (u,v) because we didn’t u s y choose it before (u,v) • Therefore (u,v) must be on the shortest path. v S 6

  7. Designing a Home Network. Attic BR3 BR2 office living 
 dining 
 BR 1 kitchen garage room room basement 7

  8. Designing a Home Network. Attic 4 4 4 2 2 BR3 BR2 office 4 4 4 4 5 4 living 
 dining 
 3 1 3 BR 1 kitchen garage room room 8 10 10 10 8 basement 8

  9. Designing a Home Network. Total cost: 62 Attic 4 BR3 BR2 office 4 4 4 living 
 dining 
 BR 1 kitchen garage room room 8 10 10 10 8 basement 9

  10. Designing a Home Network. Total cost: 44 Attic 4 2 BR3 BR2 office 4 4 living 
 dining 
 1 3 BR 1 kitchen garage room room 8 10 8 basement 10

  11. Designing a Home Network. Total cost: 32 Attic 4 2 2 BR3 BR2 office 5 4 living 
 dining 
 3 1 3 BR 1 kitchen garage room room 8 basement 11

  12. Spanning Trees • Given an undirected, connected graph G=(V,E). • A spanning tree is a tree that connects all vertices in the graph. T=(V, E T ⊆ E) v 1 v 2 v 3 v 4 v 5 v 6 v 7 12

  13. Spanning Trees • Given an undirected, connected graph G=(V,E). • A spanning tree is a tree that connects all vertices in the graph. T=(V, E T ⊆ E) T is acyclic. There is a single path between any pair of vertices. v 1 v 2 v 3 v 4 v 5 v 6 v 7 13

  14. Spanning Trees • Given an undirected, connected graph G=(V,E). • A spanning tree is a tree that connects all vertices in the graph. T=(V, E T ⊆ E) T is acyclic. There is a single path between any pair of vertices. v 1 v 1 v 2 v 4 v 3 v 2 v 3 v 4 v 5 v 6 v 7 v 5 v 6 v 7 14

  15. Spanning Trees • Given an undirected, connected graph G=(V,E). • A spanning tree is a tree that connects all vertices in the graph. T=(V, E T ⊆ E) T is acyclic. There is a single path between any pair of vertices. v 1 v 1 v 2 v 4 v 3 v 2 v 3 v 4 v 5 v 6 v 7 v 5 v 6 v 7 Any node can be the root of the spanning tree. 15

  16. Spanning Trees • Given an undirected, connected graph G=(V,E). • A spanning tree is a tree that connects all vertices in the graph. T=(V, E T ⊆ E) v 2 v 1 v 1 v 2 v 5 v 4 v 3 v 3 v 4 v 5 v 6 v 7 v 6 v 7 Number of edges in a spanning tree: |V|-1 16

  17. Spanning Trees, Applications • Constructing a computer/power networks (connect all vertices with the smallest amount of wire). • Clustering Data. • Dependency Parsing of Natural Language 
 (directed graphs. This is harder). • Constructing mazes. • … • Approximation algorithms for harder graph problems. • … 17

  18. Minimum Spanning Trees • Given a weighted undirected graph G=(E,V). • A minimum spanning tree is a spanning tree with the minimum sum of edge weights. 2 v 1 v 2 4 10 3 1 2 7 v 3 v 4 v 5 8 4 6 5 v 6 v 7 1 18

  19. Minimum Spanning Trees • Given a weighted undirected graph G=(E,V). • A minimum spanning tree is a spanning tree with the minimum sum of edge weights. 2 v 1 v 2 4 10 3 1 2 7 Total cost = 16 v 3 v 4 v 5 8 4 6 5 v 6 v 7 1 (often there are multiple minimum spanning trees) 19

  20. Prim’s Algorithm for finding MSTs • Another greedy algorithm. A variant of Dijkstra’s algorithm. • Cost annotations for each vertex v reflect the lowest weight of an edge connecting v to other vertices already visited. • That means there might be a lower-weight edge from another vertices that have not been seen yet. • Keep vertices on a priority queue and always expand the vertex with the lowest cost annotation first. 20

  21. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 ∞ 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 ∞ ∞ 2 7 While q is not empty: • v 3 v 4 v 5 (costu, u) <- q .deleteMin() 8 • 4 6 ∞ 5 if not u.visited: • 1 v 6 v 7 u.visited = True • ∞ ∞ for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 21

  22. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 4 1 2 7 While q is not empty: • v 3 v 4 v 5 (costu, u) <- q .deleteMin() 8 • 4 6 ∞ 5 if not u.visited: • 1 v 6 v 7 u.visited = True • ∞ ∞ for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 22

  23. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 2 1 2 7 While q is not empty: • v 3 v 5 v 4 (costu, u) <- q .deleteMin() 8 • 4 6 7 5 if not u.visited: • 1 v 6 v 7 u.visited = True • 4 8 for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 23

  24. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 2 1 2 7 While q is not empty: • v 3 v 5 v 4 (costu, u) <- q .deleteMin() 8 • 4 6 7 5 if not u.visited: • 1 v 6 v 7 u.visited = True • 4 8 for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 24

  25. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 2 1 2 7 While q is not empty: • v 5 v 3 v 4 (costu, u) <- q .deleteMin() 8 • 4 6 7 5 if not u.visited: • 1 v 6 v 7 u.visited = True • 4 5 for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 25

  26. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 2 1 2 7 While q is not empty: • v 5 v 3 v 4 (costu, u) <- q .deleteMin() 8 • 4 6 6 5 if not u.visited: • 1 v 6 v 7 u.visited = True • 4 1 for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 26

  27. Prim’s Algorithm Use a Priority Queue q for all v ∈ V 
 • set v.cost = ∞ , set v.visited = false Choose any vertex s. 
 • 0 2 2 set s.cost = 0, s.visited = true; v 1 v 2 q.insert(s) 
 10 • 3 4 1 2 1 2 7 While q is not empty: • v 5 v 3 v 4 (costu, u) <- q .deleteMin() 8 • 4 6 6 5 if not u.visited: • 1 v 6 v 7 u.visited = True • 4 1 for each edge (u,v): • if not v.visited: • if ( cost(u,v) < v.cost) • v.cost = cost(u,v) • v.parent = u • q.insert((v.cost,v)) • 27

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