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

data structures
SMART_READER_LITE
LIVE PREVIEW

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:


slide-1
SLIDE 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)

slide-2
SLIDE 2

Algorithm Design Methods

Greedy method. Divide and conquer. Dynamic Programming. Backtracking. Branch and bound.

slide-3
SLIDE 3

Other Methods

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

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

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

slide-6
SLIDE 6

Greedy Algorithm

slide-7
SLIDE 7

Greedy Method

  • Solve problem by making a sequence of

decisions.

  • Decisions are made one by one in some
  • rder.
  • Each decision is made using a greedy

criterion.

  • A decision, once made, is (usually) not

changed later.

slide-8
SLIDE 8

Problems: Greedy Method

  • Single Source all destination shortest path
  • Dijkstra’s Algorithm
  • Minimum Spanning Tree (MST)
  • Prim’s Algorithm
  • Bin Packing
  • Job Scheduling
slide-9
SLIDE 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.

slide-10
SLIDE 10

Shortest Path Problems

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

slide-11
SLIDE 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.
slide-12
SLIDE 12

Greedy Shortest 1 To 7 Path

1 2 3 4 5 6 7

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

slide-13
SLIDE 13

Shortest Path Algorithms

  • Dijkstra’s algorithm
  • Greedy algorithm
  • Make local decision greedily
  • Gives shortest path from a

source node

1 5 2 3 4

10 100 30 10 20 60

slide-14
SLIDE 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.

slide-15
SLIDE 15

Dijkstra’s Algorithms

50

1 5 2 3 4

10 100 30 10 20 60

  • Iter. S V-S w D[2] D[3] D[4] D[5]

Init {1} {2,3,4,5} - 10 ∞ 30 100 1 {1,2} {3,4,5} 2 10 60 30 100 2 {1,2,4} {3,5} 4 10 50 30 90 3 {1,2,4,3} {5} 3 10 50 30 60 4 {1,2,4,3,5} Φ 5 10 50 30 60

slide-16
SLIDE 16

Dijkstra’s Algorithms

  • Begin
  • S = {1}
  • For I = 2 to n do

– D[i] = C [1,i] – initialize

  • For i = 1 to n-1 do begin

– Choose a vertex w in V-S s.t. D[w] is minimum – Add w to S – For each vertex v in V-S do – D[v] = Min{D[v], D[w]+C[w,v]}

  • end

1 5 2 3 4

10 100 30 10 20 60

slide-17
SLIDE 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).
slide-18
SLIDE 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

slide-19
SLIDE 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.
slide-20
SLIDE 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

slide-21
SLIDE 21

Prim’s Algorithm

1 4 2 5 6

6 5 3 4 6 2

3

1 5 5 6 2

1 4 2 5 6

3 4

3

1 5

slide-22
SLIDE 22

Prim’s Algorithm

1 4 2 5 6

6 5 3 4 6 2

3

1 5 5 6 1 4 2 5 6 3 1 1 4 2 5 6 2 4 3 1 5 1 4 2 5 6 4 3 1 1 4 2 5 6 2 4 3 1 1 4 2 5 6 3 4 3 1 5 2

slide-23
SLIDE 23

Thank You