Design and Analysis of Computer Algorithm Additional Note

  Design and Analysis of Computer Algorithm Additional Note

  Pradondet Nilagupta Department of Computer Engineering

  This lecture note has been modified from lecture note for 23250 by Prof. Francis Chin , CS332 by David Luekbe

  Design and Analysis of Computer Algorithm

  Design and Analysis of Computer Algorithm March 17, 2009 Greedy Method (Cont.)

  Single Source Shortest Path Given a (un)weighted, directed graph G = {V, E}, in

  ● which each edge has a non-negative cost (weight)

  ● Problem: Determine the cost of the shortest path from the source vertex to every other vertex V

  

■ The length of a path is the sum of the costs of the

edges on the path Why not find path to just one particular destination?

  ■

● Application: Think of G as a map of airline flights, want to

find the minimum travel time from a given city to every

other city on the map

  March 17, 2009 Design and Analysis of Computer Algorithm Dijkstra’s Algorithm Maintain a set S of vertices whose shortest distance from

  ● the source is already known

  ● Initially S contains only source vertex

At each step, we add to S a remaining vertex w whose

  ● distance from the source is as short as possible Assuming all edges have non-negative costs, we can

  ● always find a shortest path from the source to v that passes only through vertices in S (special path) At every step we use an array to record the length of the

  ● shortest special path to each vertex

  ● Done, when S includes all vertices (all paths are special) March 17, 2009 Design and Analysis of Computer Algorithm Design and Analysis of Computer Algorithm March 17, 2009 An Example inf

  2

  7

  5

  4

  1

  4

  3

  

4

  1

  2 inf a d

  5 inf b f s inf

  7 e c inf inf

  2 inf

  7 a d

  5

  2

  2

  4

  5

  5

  1 b f

  

3

s inf

  1

  7

  4 e c

  4 inf

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  9

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s inf

  1

  7

  4 e c

  4 inf

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  8

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s inf

  1

  7

  4 e c

  4

  7

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  8

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s inf

  1

  7

  4 e c

  4

  7

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  8

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s

  14

  1

  7

  4 e c

  4

  7

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  8

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s

  13

  1

  7

  4 e c

  4

  7

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  2

  8

  7 a d

  5

  2

  2

  4

  4

  5

  1 b f

  3 s

  13

  1

  7

  4 e c

  4

  7

4 March 17, 2009

  Design and Analysis of Computer Algorithm

  Shortest Path Tree The unique simple path from s to v in the tree is a shortest path from s to v.

  2

  8 a d

  5

  2

  2

  4

  4 b f

  3 s

  13

  4 e c

  7 March 17, 2009 Design and Analysis of Computer Algorithm Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  ∞ s

  13

  23

  2

  1

  5

  3

  14

  6

  8

  9

  10

  12

  ,

  1

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  ∞ s

  13

  23

  2

  1

  5

  3

  14

  6

  8

  9

  10

  12

  ,

  1

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞ s

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  14

  ,

  9

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞ s

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  14

  ,

  9

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞ s

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  ,

  9

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞ s

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  ,

  9

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 ∞ s

  u

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  );

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  k

  v

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  ,

  9

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  23 ∞ Design and Analysis of Computer Algorithm March 17, 2009

  2 s

  u

  k

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  v

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  );

  ,

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  v

  9

  15

  18

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  23 Design and Analysis of Computer Algorithm March 17, 2009

  2 s

  u

  k

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  v

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  );

  ,

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  v

  9

  15

  18

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  23 Design and Analysis of Computer Algorithm March 17, 2009

  2 s

  u

  k

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  v

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  );

  ,

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  v

  9

  15

  18

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  23 Design and Analysis of Computer Algorithm March 17, 2009

  2 s

  u

  k

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  v

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  );

  ,

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  v

  9

  15

  18

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  23 Design and Analysis of Computer Algorithm March 17, 2009

  2 s

  u

  k

  [ source ] ← 0; Build a heap for all values in the array k.

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  ], then reduce k[

  v

  u

  ] to k[v] + weight_of ( (v,

  u

  ) ), and

  n

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  4

  );

  ,

  23

  1

  2

  1

  5

  3

  14

  6

  8

  9

  10

  13

  12

  7

  v

  9

  15

  18

  for each

  v

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  21 Design and Analysis of Computer Algorithm March 17, 2009

  for each

  14

  [

  u

  ] ← (v ,

  u ).

  end of for-loop Until | B | == | V |.

  2 s

  23

  2

  1

  5

  3

  6

  ) ), and

  8

  9

  10

  13

  12

  1

  7

  9

  15

  18

  4

  n

  u

  v

  k

  ∈V,

  k

  [

  v

  ] ← infinity; n[

  v

  ] ← (

  v

  ,

  v

  );

  [ source ] ← 0; Build a heap for all values in the array k.

  ] to k[v] + weight_of ( (v,

  S ← {}, T ← {}.

  Repeat Find the vertex v with minimum value in heap using extract_min.

  Insert v into S. Insert n[v] into T For each

  

u

  such that (v,

  u

  ) ∈ E, if k[v] plus the weight of (v,

  u

  ) is less than k[

  u

  ], then reduce k[

  u

  21