cs 332 algorithms n.
Download
Skip this Video
Download Presentation
CS 332: Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 26

CS 332: Algorithms - PowerPoint PPT Presentation


  • 135 Views
  • Uploaded on

CS 332: Algorithms. Review of MST Algorithms Disjoint-Set Union Amortized Analysis. Review: MST Algorithms. In a connected, weighted, undirected graph, will the edge with the lowest weight be in the MST? Why or why not? Yes:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 332: Algorithms' - early


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 332 algorithms

CS 332: Algorithms

Review of MST Algorithms

Disjoint-Set Union

Amortized Analysis

David Luebke 16/9/2014

review mst algorithms
Review: MST Algorithms
  • In a connected, weighted, undirected graph, will the edge with the lowest weight be in the MST? Why or why not?
  • Yes:
    • If T is MST of G, and A  T is a subtree of T, and (u,v) is the min-weight edge connecting A to V-A, then (u,v)  T
    • The lowest-weight edge must be in the tree (A=)

David Luebke 26/9/2014

review mst algorithms1
Review: MST Algorithms
  • What do the disjoint sets in Kruskal’s algorithm represent?
  • A: Parts of the graph we have connected up together so far

David Luebke 36/9/2014

kruskal s algorithm
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 46/9/2014

kruskal s algorithm1
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1?

David Luebke 56/9/2014

kruskal s algorithm2
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 66/9/2014

kruskal s algorithm3
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2?

19

9

14

17

8

25

5

21

13

1

David Luebke 76/9/2014

kruskal s algorithm4
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 86/9/2014

kruskal s algorithm5
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5?

21

13

1

David Luebke 96/9/2014

kruskal s algorithm6
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 106/9/2014

kruskal s algorithm7
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8?

25

5

21

13

1

David Luebke 116/9/2014

kruskal s algorithm8
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 126/9/2014

kruskal s algorithm9
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9?

14

17

8

25

5

21

13

1

David Luebke 136/9/2014

kruskal s algorithm10
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 146/9/2014

kruskal s algorithm11
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13?

1

David Luebke 156/9/2014

kruskal s algorithm12
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 166/9/2014

kruskal s algorithm13
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14?

17

8

25

5

21

13

1

David Luebke 176/9/2014

kruskal s algorithm14
Kruskal’s Algorithm

Run the algorithm:

Kruskal()

{

T = ;

for each v  V

MakeSet(v);

sort E by increasing edge weight w

for each (u,v)  E (in sorted order)

if FindSet(u)  FindSet(v)

T = T U {{u,v}};

Union(FindSet(u), FindSet(v));

}

2

19

9

14

17

8

25

5

21

13

1

David Luebke 186/9/2014

review shortest path algorithms
Review: Shortest-Path Algorithms
  • How does the Bellman-Ford algorithm work?
  • How can we do better for DAGs?
  • Under what conditions can we use Dijkstra’s algorithm?

David Luebke 196/9/2014

review running time of kruskal s algorithm
Review: Running Time ofKruskal’s Algorithm
  • Expensive operations:
    • Sort edges: O(E lg E)
    • O(V) MakeSet()’s
    • O(E) FindSet()’s
    • O(V) Union()’s
  • Upshot:
    • Comes down to efficiency of disjoint-set operations, particularly Union()

David Luebke 206/9/2014

disjoint set union
Disjoint Set Union
  • So how do we represent disjoint sets?
    • Naïve implementation: use a linked list to represent elements, with pointers back to set:
      • MakeSet(): O(1)
      • FindSet(): O(1)
      • Union(A,B): “Copy” elements of A into set B by adjusting elements of A to point to B: O(A)
    • How long could n Union()s take?

David Luebke 216/9/2014

disjoint set union analysis
Disjoint Set Union: Analysis
  • Worst-case analysis: O(n2) time for n Union’s

Union(S1, S2) “copy” 1 element

Union(S2, S3) “copy” 2 elements

Union(Sn-1, Sn) “copy” n-1 elements

O(n2)

  • Improvement: always copy smaller into larger
    • How long would above sequence of Union’s take?
    • Worst case: n Union’s take O(n lg n) time
    • Proof uses amortized analysis

David Luebke 226/9/2014

amortized analysis of disjoint sets
Amortized Analysis of Disjoint Sets
  • If elements are copied from the smaller set into the larger set, an element can be copied at most lg n times
    • Worst case: Each time copied, element in smaller set

1st time resulting set size  2

2nd time  4

(lg n)th time  n

David Luebke 236/9/2014

amortized analysis of disjoint sets1
Amortized Analysis of Disjoint Sets
  • Since we have n elements each copied at most lg n times, n Union()’s takes O(n lg n) time
  • Therefore we say the amortized cost of a Union() operation is O(lg n)
  • This is the aggregate method of amortized analysis:
    • n operations take time T(n)
    • Average cost of an operation = T(n)/n

David Luebke 246/9/2014

amortized analysis accounting method
Amortized Analysis: Accounting Method
  • Accounting method
    • Charge each operation an amortized cost
    • Amount not used stored in “bank”
    • Later operations can used stored money
    • Balance must not go negative
  • Book also discusses potential method
    • But we won’t worry about it here

David Luebke 256/9/2014

the end
The End

David Luebke 266/9/2014

ad