data structures
play

Data Structures Graph Algorithms Virendra Singh Associate Professor - PowerPoint PPT Presentation

Data Structures Graph Algorithms Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail:


  1. Data Structures Graph Algorithms Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail: viren@ee.iitb.ac.in EE-717/453:Advance Computing for Electrical Engineers Lecture 11 (29 Aug 2013)

  2. Algorithm Design Methods  Greedy method.  Divide and conquer.  Dynamic Programming.  Backtracking.  Branch and bound.

  3. Other Methods  Linear Programming.  Integer Programming.  Simulated Annealing.  Neural Networks.  Genetic Algorithms.  Tabu Search.

  4. Optimization Problem A problem in which some function (called the optimization or objective function) is to be optimized (usually minimized or maximized) subject to some constraints.

  5. Feasible And Optimal Solutions A feasible solution is a solution that satisfies the constraints. An optimal solution is a feasible solution that optimizes the objective/optimization function.

  6. Greedy Algorithm

  7. Greedy Method  Solve problem by making a sequence of decisions.  Decisions are made one by one in some order.  Each decision is made using a greedy criterion.  A decision, once made, is (usually) not changed later.

  8. Problems: Greedy Method  Single Source all destination shortest path  Dijkstra’s Algorithm  Minimum Spanning Tree (MST)  Prim’s Algorithm  Bin Packing  Job Scheduling

  9. Application: Shortest Path Problems  Directed weighted graph.  Path length is sum of weights of edges on path.  The vertex at which the path begins is the source vertex.  The vertex at which the path ends is the destination vertex.

  10. Shortest Path Problems  Single source single destination.  Single source all destinations.  All pairs (every vertex is a source and destination).

  11. Single Source Single Destination Possible greedy algorithm:  Leave source vertex using cheapest/shortest edge.  Leave new vertex using cheapest edge subject to the constraint that a new vertex is reached.  Continue until destination is reached.

  12. Greedy Shortest 1 To 7 Path 8 6 2 1 3 3 1 16 7 5 6 4 10 4 2 4 7 5 3 14 Path length is 12. Not shortest path. Algorithm doesn’t work!

  13. Shortest Path Algorithms 1 10 100 • Dijkstra’s algorithm 30  Greedy algorithm 2 5  Make local decision greedily 10 60  Gives shortest path from a 4 3 source node 20

  14. Data Structures For Dijkstra’s Algorithm • The greedy single source all destinations algorithm is known as Dijkstra’s algorithm. • Implement d() and p() as 1D arrays. • Keep a linear list L of reachable vertices to which shortest path is yet to be generated. • Select and remove vertex v in L that has smallest d() value. • Update d() and p() values of vertices adjacent to v.

  15. Dijkstra’s Algorithms Iter. S V-S w D[2] D[3] D[4] D[5] 1 10 100 Init {1} {2,3,4,5} - 10 ∞ 30 100 30 2 5 1 {1,2} {3,4,5} 2 10 60 30 100 10 50 2 {1,2,4} {3,5} 4 10 50 30 90 60 4 3 3 {1,2,4,3} {5} 3 10 50 30 60 20 4 {1,2,4,3,5} Φ 5 10 50 30 60

  16. Dijkstra’s Algorithms • Begin • S = {1} 1 • For I = 2 to n do 10 100 – D[i] = C [1,i] – initialize 30 2 5 • For i = 1 to n-1 do begin 10 – Choose a vertex w in V-S s.t. D[w] is minimum 60 – Add w to S 4 3 20 – For each vertex v in V-S do – D[v] = Min{D[v], D[w]+C[w,v]} • end

  17. Complexity • O(n) to select next destination vertex. • O(out-degree) to update d() and p() values when adjacency lists are used. • O(n) to update d() and p() values when adjacency matrix is used. • Selection and update done once for each vertex to which a shortest path is found. • Total time is O(n2 + e) = O(n2).

  18. Minimum-Cost Spanning Tree  weighted connected undirected graph  Free tree that connects all the vertices  spanning tree  cost of spanning tree is sum of edge costs  find spanning tree that has minimum cost

  19. Edge Selection Greedy Strategies • Start with a 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex and an edge. When there is a choice, add a least cost edge.  Prim’s method. • Start with an n-vertex 0-edge forest. Consider edges in ascending order of cost. Select edge if it does not form a cycle together with already selected edges.  Kruskal’s method.

  20. MST: Prims Algorithms • Prim’s Algorithm  Greedy algoritthm  Start from an intial node U = {1}  Grows ST, one edge at a time  At each step, it finds a shortest edge (u,v) that connects U and V-U and adds v to V-U from U

  21. Prim’s Algorithm 1 1 6 5 1 1 5 5 5 2 4 2 4 3 3 4 4 6 3 3 2 2 6 6 5 5 6

  22. Prim’s Algorithm 1 1 1 6 5 1 1 1 5 2 4 5 3 2 4 2 4 3 3 4 4 6 3 2 6 5 6 5 6 5 6 1 1 1 1 1 5 1 5 2 4 2 4 3 3 2 4 3 4 4 2 2 4 3 2 6 6 6 5 5 5

  23. Thank You

Recommend


More recommend