Docsity

Notes on Algorithm and Data Structure | CS 473, Study notes of Algorithms and Programming

Partial preview of the text

Download Notes on Algorithm and Data Structure | CS 473 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 473ug: Algorithms

Mahesh Viswanathan [email protected] 3232 Siebel Center University of Illinois, Urbana-Champaign

Spring 2007

Data Structures

Part I

Data Structures

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Prim’s Algorithm

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) while S =/= V pick e = (v,w) in E such that v ∈ S and w ∈ V - S e has minimum cost T = T U e S = S U w return the set T Analysis Number of iterations = O(n), where n is number of vertices

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Prim’s Algorithm

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) while S =/= V pick e = (v,w) in E such that v ∈ S and w ∈ V - S e has minimum cost T = T U e S = S U w return the set T Analysis Number of iterations = O(n), where n is number of vertices Picking e is O(m) where m is the number of edges

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

More Efficient Implementation

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) for v 6 ∈ S, a(v) = minw ∈S c(w,v) for v 6 ∈ S, e(v) = w such that w ∈ S and c(w,v) is minimum while S =/= V pick v with minimum a(v) T = T U (e(v),v) S = S U v update arrays a and e return the set T

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

More Efficient Implementation

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) for v 6 ∈ S, a(v) = minw ∈S c(w,v) for v 6 ∈ S, e(v) = w such that w ∈ S and c(w,v) is minimum while S =/= V pick v with minimum a(v) T = T U (e(v),v) S = S U v update arrays a and e return the set T

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Priority Queues

Data structure to store a set S of n elements where each element v ∈ S has an associated key k(v ) such that the following operations extractMin: Remove v ∈ S with smallest key add: Add an element to set S delete: Remove element from S changeKey: Change the key associated with an element can be performed in O(log n) time each.

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Priority Queues

Data structure to store a set S of n elements where each element v ∈ S has an associated key k(v ) such that the following operations extractMin: Remove v ∈ S with smallest key add: Add an element to set S delete: Remove element from S changeKey: Change the key associated with an element can be performed in O(log n) time each.

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Prim’s using priority queues

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) for v 6 ∈ S, a(v) = minw ∈S c(w,v) for v 6 ∈ S, e(v) = w such that w ∈ S and c(w,v) is minimum while S =/= V pick v with minimum a(v) T = T U (e(v),v) S = S U v update arrays a and e return the set T Maintain vertices in V \ S in a priority queue with key a(v ) Requires O(n) extractMin operations

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Prim’s using priority queues

E is the set of all edges in G S = { 1 } T is empty (* T will store edges of a MST *) for v 6 ∈ S, a(v) = minw ∈S c(w,v) for v 6 ∈ S, e(v) = w such that w ∈ S and c(w,v) is minimum while S =/= V pick v with minimum a(v) T = T U (e(v),v) S = S U v update arrays a and e return the set T Maintain vertices in V \ S in a priority queue with key a(v ) Requires O(n) extractMin operations Requires O(m) changeKey operations

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Priority Queues

Using Lists Store elements in a list, and have pointer to element with smallest key

  • Adding elements can be done in O(1) time

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Priority Queues

Using Lists Store elements in a list, and have pointer to element with smallest key

  • Adding elements can be done in O(1) time
  • If we remove the minimum element, updating min pointer takes O(n) time

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Priority Queues

Using Lists Store elements in a list, and have pointer to element with smallest key

  • Adding elements can be done in O(1) time
  • If we remove the minimum element, updating min pointer takes O(n) time Using Sorted Arrays Store elements in a sorted array based on keys
  • Removing element involves removing first element.

Data Structures Implementing Kruskal’s AlgorithmUnion-Find Data Structure

Implementing Priority Queues

Using Lists Store elements in a list, and have pointer to element with smallest key

  • Adding elements can be done in O(1) time
  • If we remove the minimum element, updating min pointer takes O(n) time Using Sorted Arrays Store elements in a sorted array based on keys
  • Removing element involves removing first element.
  • Adding elements takes long! Can find the position of new element in O(log n) time, but shifting elements requires O(n) time.