CS 477 -- Algorithm Analysis


Text: Horowitz, et al. Computer Algorithms C++
Back to Homepage
Back to ACM Page

 

Searching
sorting
Graph Traversal
Dynamic Programming








Search Algorithms

Binary Search - source code (cpp , java)
  • Algorithm: to search for x in a[0...n]
    • a[] has to be sorted in nondecreasing order.
    • compare x with a[n/2]
      • if x = = a[n/2], then x is found
      • else if x < a[n/2], then do binary search on a[0...n/2]
      • else if x > a[n/2], then do binary search on a[n/2...n]
  • Divide-And-Conquer paradigm
  • Time Complexity
    • successful searches
      • average, worst: Θ(log n)
      • best Θ(1)
    • unsuccessful searches
      • best, average, worst: Θ(log n)
  • Required that list must be sorted in nondecreasing order

 

Sorting Algorithms

Merge Sort - source code (cpp)
  • Algorithm to sort a[0...n] in nondecreasing order
    • divide the array into half.
    • recursively sort left side
    • recursively sort right side
    • merge left and right
  • Divide-And-Conquer paradigm
  • Time Complexity
    • worst, average, best: O(n log n)
  • Cons
    • requires 2n space for a temporary array
    • uses Recursion, log n stack space
  • good to use in large data sets.
Insertion Sort - source code (cpp cpp2)
  • Algorithm to sort a[0...n] in nondecreasing order
    • Find minimum of a[0...n] and store it at a[0].
    • for (j=1, j<n; j++)
      • place a[j] in its correct position in the sorted set a[0...j-1]
  • Time Complexity
    • worst: O(n2); -- sorted in decreasing order
    • best: Θ(n); -- sorted in nondecreasing order
  • Good only for small data sets
Quick Sort - source code (java)
  • Algorithm to sort a[0...n] in nondecreasing order
    • Uses partitioning scheme
      • selects a pivot.
      • everything to the left of the pivot is smaller than the pivot
      • everything to the right of the pivot is larger than the pivot
    • First, partition among a pivot j. Upon completion, we know everything left of j is less than j, and everything right of j is greater than j
    • Then, recursively partition left and right side of j.
  • Time Complexity
    • worst: O(n2); -- sorted in nondecreasing order, with first element as pivot
    • average, best:O(n log n); -- values are in random order
  • Space Complexity
    • worst: n-1 stack space needed for recursion
  • Use random pivots to break the chances of dealing with the worst case (randomized quicksort)
  • Good on random data of all sizes, preferably large data sets
  • Extremely fast in practical applications
Selection - source code (java)
  • Algorithm to select the kth smallest item of a set
    • Uses paritioning scheme as in quicksort
    • select a pivot, then partition. The position of the pivot is at a[j].
      • if j == k, then the pivot is the kth smallest
      • if k < j, then partition a[0...j-1]
      • if k > j, then partition a[j+1...n]
  • Time Complexity
    • Worst Case: O(n2)
      • k = n and the elements are arranged so that the pivot on the ith call to partition() is the ith smallest number
    • Best, Average Case: O(n)

 

Graph Traversal Algorithms

DFS - source code (cpp)
  • Algorithm for general graphs
    • DFS starts the search from a starting vertex v0
    • When a vertex is reached, it is marked "visited" and the search starts from an unmarked vetex w that is adjacent from v
    • The search continues until a dead-end vertex is reached
      • i.e. a vertex whose all adjacent vertices are marked "visited"
    • At the dead-end, the algorithm backs up along the last edge traversed and starts exploring from there.
  • Tree is implemented using an adjacency matrix
    • space complexity O( V2 )
    • edge detection time O(1)
  • n nodes will make n calls to DFS()
  • All edges are visited exactly once
  • Node 0 is the starting point
  • the program outputs the original graph followed by all possible DFS trees with its corresponding component
  • Time Complexity of algorithm: Θ( n2 )
  • Space Complexity of algorithm: Θ (n)
  • Program output

BFS - source code (java)

  • Algorithm for general graphs
    • Def: i is explored when all nbrs of i are visited.
    • Start at a random node i.
    • visit all the neighbors of node i.
      • add each neighbor visited, but unexplored to a queue
    • Now node i is explored.
    • Pop the first element off the queue and repeat the process.
  • Tree is implemented using an adjacency matrix
  • Time Complexity: Θ( n2 )
  • Space Complexity: Θ (n)
  • Program output
Prim's MST
Kruskal mst
Dijkstra SP
bellmanford sp

 

Dynamic Programming

 

Others:

 

 

Notes:

 

Please report all broken links to jam0cam@yahoo.com

Created 2/2/06

Last Updated: 7/20/06