Graphs, Dijkstra's shortest path algorithm

Sorting Review

introspectiveSort: (History -- it is a lot newer than the others)   Sorters.h

Is like quickSort, but use insertionSort for the smaller parts and revert to heapSort if recursion depth gets too deep.

Pseudocode: introspectiveSort(a, n)

  1. If n is small, sort by insertionSort, which is fast for small n but awful for large n, O(n2).
  2. If recursion depth of calls to introspectiveSort is more than 2.5log(n) sort by heapSort
  3. Otherwise continue with divide and conquer
    1. partition a using a random pivot.
    2. introspectiveSort the items less than the pivot.
    3. introspectiveSort the items greater than the pivot.

Result: An in-place sorting algorithm with worst case runtime of O(n log(n)) and expected case runtime of quickSort_T. ...not just O(n log(n)) expected time, but the actual fast time of quickSort_T.



Sorter in place asymptoticspragmatics
insertionSort yes awful:(O(n2)) fast for small n
mergeSort no worst case O(n log(n)) near best, has special uses
quickSort yes expected case O(n log(n)) best in practice, give or take
heapSort yes worst case o(n log(n)) factor of two or three worse than best
introspectiveSort yes worst case O(n log(n)) best in practice, give or take





Graphs.

Graphs have vertices and edges. Each edge connects two vertices.

For example a road network is a graph with intersections as vertices and road segments between intersections as edges.

For example a social network is a graph, with people as vertices and "friend" or "like" relationships represented by edges.

For example the internet itself is a graph with devices and routers as vertices and connecting links as edges.

Sometimes edges are given weights (or costs), think for example of the length of a road segment in a road network as the edge weight.

Sometimes graphs are directed (edge (a,b) represents a link from a to b, and not vice versa), and sometimes graphs are undirected ( where (a,b) and (b,a) denote the same bidirectional edge). As is commonly done, we will represent undirected graphs by incorporating both directions of an edge in the data structure.

The most common data structures used to represent graphs are the adjacency matrix and the (array of) adjacency lists. See ODS chapter 12.1, 12.2.

Graphs are ubiquitous --- they represent all binary relationships for starters --- and there are many, many important computational problems concerning graphs. They are the core subject of CISC 320 Algorithms. We will consider one fundamental problem: distance between vertices.

How can one determine the shortest path between vertex s and vertex d in a graph? (s is for "source", d for "destination".)

It turns out the best general method is to learn the shortest path from s to every vertex in the graph. This is called the single source shortest path, SSSP, problem.


A breadth first traversal of the graph starting at s in which we learn the distances of vertices from s.

  1. Queue<vertex> Q; array<float> d(n); //d is distance to start vertex s.
    For all vertices v do d[v] = infinity;
    d[s] = 0; Mark s as "core"; Q.enqueue(s); // Core vertices v have d[v] correctly stating the shortest distance to s.
  2. while queue is not empty do:
        u = Q.dequeue();
        For each neighbor v of u do
            if ( v is not yet core )
                d[v] = d[u] + 1;
                Mark v as "core";
                Q.enqueue(v).