Educational_Resources

Lecture 5: Graph Algorithms

Created By: Yusuf Pisan

formatted to Github Markdown syntax by Ryan Peters

Be sure to check the other lectures out after you finish this one!

Previous lecture Next lecture

Table of Contents

Overview


Overview


Ass1: Initial Feedback


Ass2: BinarySearchTree

template<class ItemType>
void BinarySearchTree<ItemType>::inorderTraverse(void visit(ItemType&)) const {
}  // end inorder

visit is a function that takes a single argument of ItemType

void itemDisplay(string& anItem) {
    cout << anItem << " ";
}

// bst.inorderTraverse(itemDisplay);

Graph Algorithms: DFS + BFS

Group Exercise: Looking for a path from e to i. List the order of vertices visited and stack/queue at each iteration


Graph Algorithms: Djikstra’s Shortest Path

To find the shortest path from one vertex to all other vertices

// finding shortest-paths from vertex 0
weight[v] = matrix[0][v] for all v
// weight[0] = "-", [1] = 8, [2] = -, [3] = 9, [4] = 4
add v (vertex 0) to vertexSet
while there are still vertices not in vertexset (do this n-2 times)
    Find v with smallest weight v
    Add v to vertexSet
    for all u not in vertexSet
        // if going through the newly selected v will make it shorter
        // set the new weight
        if (weight[u] > weight[v] + matrix[v][u])
            weight[u] = weight[v] + matrix[v][u])

Group Exercise: Find the shortest-distance from node-3 to all other nodes. Write out your vertexSet at each step.


Graph Algorithms: Uniform Cost Search (UCS)

Maintain a priority queue

Insert the root into the queue
While the queue is not empty
      Dequeue the maximum priority element from the queue
      If the path is ending in the goal state
            Print the path and exit
      Else
            Insert all the children of the dequeued element, 
            with the cumulative costs as priority

Path from 0 to 1

    node = 0
    queue = { {0->4, 4}, {0->1, 8}, {0->3, 9} }

    path = {0->4, 4}
    queue = { {0->4->2, 5}, {0->1, 8}, {0->3, 9} }
    
    path = {0->4->2, 5}
    queue = { {0->4->2->1, 7}, {0->4->2-3, 8}, {0->1, 8}, {0->3, 9} }
    
    path = {0->4->2->1, 7}
    reached goal 1, minimum path cost is 7

If all edges have a cost of 1, UCS is the same as BFS

If the priority is set to the numberOfNodesInPath, UCS is the same as DFS


Graph Algorithms: A*

Maintain a priority queue with a heuristic

f(n) = g(n) + h(n)
g(n) actual cost of getting to node n
h(n) heuristic cost of getting from n to goal

The quality of the heuristic determines how much better A* is from UCS

If h(n) is 0, A* is the same as UCS

Admissable heuristic: Must underestimate, must be less than actual possible cost


Topological Sort

Directed graph without cycles has a topological order.

x precedes y if there is a an edge from x to y

142 -> 143 -> 342 -> 343 143 -> 295 143 -> 340 -> 385 143 -> 342 -> 385 132 -> 133 142 -> 173 142 -> 305 142 -> 337 143 -> 340 301 -> 351 301 -> 370 342 -> 421 ENGL 182 -> 301

We cannot represent AND prerequisites (MATH 125 AND CSS 340)

We cannot represent “must be taken concurrently”

For scheduling, we want to generate a topological order

If all pairs of consecutive vertices in the sorted order are connected by edges, then the edges form a Hamiltonian path

Exercise: Draw a square without lifting pen, draw a house, …


Travelling Salesman Problem (TSP)

Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point.

Paths: ABCDE, ABCED, ABDCE, ABDEC, .

Complexity: O(n-1)! based on the number of paths

but we can do better with dynamic programming

For a set of size n, we consider n-2 subsets each of size n-1 such that all subsets don’t have nth in them.

There are at most O(n * 2^n^) subproblems, and each one takes linear time to solve

The total running time is therefore O(n^2^ * 2^n^)

https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/


xkcd: Travelling Salesman

https://xkcd.com/399/


Spanning Trees

A tree is a psecial kind of undirected graph, connected but no cycles

Depth-First search spanning tree: Traverse the tree marking edges. When DFS ternminates, the marked edges make up a spanning tree

Breadth-First search spanning tree: Traverse the tree marking edges. When BFS ternminates, the marked edges make up a spanning tree

Minimum spanning tree: A spanning tree that where the sum of the edges is minimal - Ethernet line to all the outlets in your home with minimal cabling

Group Exercise: Construct DFS, BFS and minimum spanning tree (starting from e for DFS and BFS)


Prim’s Algorithm

Start at any vertex, r
mark r as visited
while (there are unvisited vertices)
    find the least-cost edge (v, u) from some visited vertex v to u
    mark u as visited
    Add the vertex u and the edge (v, u) to minimum spanning tree

Big O

28 Examples from “Cracking The Coding Interview”


Big O (2)

How many questions will be asked?


Big O (3)

Two loops in a row:
for (i = 0; i < N; i++) {
    sequence of statements
}
for (j = 0; j < M; j++) {
    sequence of statements
}

A nested loop followed by a non-nested loop:
for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
        sequence of statements
    }
}
for (k = 0; k < N; k++) {
    sequence of statements
}
A nested loop in which the number of times the inner loop executes depends on the value of the outer loop index:
for (i = 0; i < N; i++) {
    for (j = N; j > i; j--) {
        sequence of statements
    }
}

// nested loop is n*n
for (int    i   =   0;  i   <   n;  i++)    {
        for (int    j   =   0;  j   <   n*n;    j++)    {
                cout    <<  “tricky!”   <<  endl;
        }
}

Big O (4)

f takes constant time, g takes time linear based on the value of its parameter.

1. for (j = 0; j < N; j++) f(j);

2. for (j = 0; j < N; j++) g(j);

3. for (j = 0; j < N; j++) g(k);


Big O (5)

Dynamicc programming stores and reuses results

RE-writing fibonacci, so it is not recurive.

What is the complexity?


Big O (6)

Knapsack problem: Objects with given size and value, maximize value taht can be stored in capacity C


Big O (7)


Big O (8)

Shortest Path: Djikstra’s algorithm to find shortest path for all nodes

To find the shortest path from one vertex to all other vertices

// finding shortest-paths from vertex 0
weight[v] = matrix[0][v] for all v
// weight[0] = "-", [1] = 8, [2] = -, [3] = 9, [4] = 4
add v (vertex 0) to vertexSet
while there are still vertices not in vertexset (do this n-2 times)
    Find v with smallest weight v
    Add v to vertexSet
    for all u not in vertexSet
        // if going through the newly selected v will make it shorter
        // set the new weight
        if (weight[u] > weight[v] + matrix[v][u])
            weight[u] = weight[v] + matrix[v][u])

Big O (9)

Sort each tring in an array and then sort the array (Example 8 from Cracking)


After Class