Graph traversal algorithms

Other graph algorithms

Brute-force algorithms

Test every possible case

Some examples:

Traveling Salesman Problem (TSP)

Knapsack Problem

Greedy algorithms

At each step, select the option that is locally the best to find the overall optimal solution.

Example

Activities problem

Fractional Knapsack

Correctness of greedy algorithms

The greedy-choice property

A globally optimal solution that can be arrived at by making a locally optimal (greedy) choice.

The optimal substructure property

An optimal solution to the problem contains within it optimal solutions to subproblems.

Flow algorithms

Max flow

Ford-Fulkerson

Edmonds-Karp

Maximal bipartite matching

WIP

Minimum Cost Spanning Trees (MST)

Given an undirected connected graph G, a spanning tree is an acyclic subset of the edges that connect all the vertices. The cost of the spanning tree is the sum of the costs of its edges.

A MST has 2 properties:

Kruskal's Algorithm

Prim's Algorithm

function MST-Prim(G,w,r)
Q = V[G]; // Priority queue Q
foreach u Q do // Initialization
key[u] = ;
key[r] = 0;
pred[r] = NIL; // Keep track of tree A
while Q do
u = ExtractMin(Q); // Pick the closest unprocessed node
// (u,v) safe and light edge, for tree A
foreach v Adj[u] do
if (v Q and w(u,v) < key[v]) then // Check if node is not in MST
pred[v] = u;
key[v] = w(u,v); // Min Heap Q is updated!

std::vector<Vertex<T> *> prim(Graph<T> *g) {  
    MutablePriorityQueue<Vertex<T>> q;  
    // First insert all vertices into the queue, with the distance set as infinite (int max).  
    for (Vertex<T> *v: g->getVertexSet()) {  
        v->setDistmax();  
        v->setProcesssing(true);  
        q.insert(v);  
    }    // Set the distance of the first vertex to 0 so that it is used first.  
    vector<Vertex<T> *> res;  
    Vertex<T> *firstVertex = g->getVertexSet()[0];  
    firstVertex->setDist(0);  
    q.decreaseKey(firstVertex);  
  
    // While there are vertices in the queue, extract the one with the lowest distance.  
    while (!q.empty()) {  
        Vertex<T> *vertex = q.extractMin();  
        vertex->setProcesssing(false);  
        res.push_back(vertex);  
        vertex->setVisited(true);  
  
        // For each adjacent vertex, if it is inside the queue and the weight of the edge is lower than the distance, update the path and the distance.  
        for (Edge<T> *edge: vertex->getAdj()) {  
            Vertex<T> *adjacentVertex = edge->getDest();  
            if (adjacentVertex->isProcessing() && edge->getWeight() < adjacentVertex->getDist()) {  
                adjacentVertex->setPath(edge);  
                adjacentVertex->setDist(edge->getWeight());  
                q.decreaseKey(adjacentVertex);  
            }        }    }    return res;  
}

Shortest Path

Dijkstra's Algorithm (SSSP)

Find the shortest path from one vertex to every other vertex

Bellman-Ford-Moore Algorithm (SSSP)

Shortest Path in DAG (SSSP)

Johnson's Algorithm (ASSP)

Divide & Conquer

The Master Theorem

T(n)={dif n is at most some constantaT(nb)+f(n)otherwiseT(n)={O(nc)logba<cO(nclogn)logba=cO(nlogba)logba>c