Skip to main content

Section 3.2 Discrete Algorithms

Problem 3.2.1. Depth first search.

Use the following algorithm to find a way through the maze FigureĀ 3.2.2.

  1. Move along any corridor where there is no choice.

  2. At each intersection where a choice must be made choose the rightmost option of those not yet tried.

  3. If you reach a dead end, back up to the immediately previous choice.

Figure 3.2.2. Maze

Problem 3.2.3. Breadth first search.

Use this algorithm to find a way through the maze in FigureĀ 3.2.2.

Move along any corridor where there is no choice. At each intersection where a choice must be made choose all of the options. This means you are following (tracking) more than one path at the same time. Repeat this process level by level until the solution is found.

Problem 3.2.4.

Which method produced the solution with the fewest steps? Explain

Problem 3.2.5.

What did the breadth first method produce (or almost produce) that the depth first method did not?

Definition 3.2.6.

A graph with weighted edges is a network.

Figure 3.2.8. Network for Shortest Paths

Problem 3.2.9.

Use the Greedy algorithm to find a path from \(x_1\) to \(x_{10}\) in FigureĀ 3.2.8.

Solution.

Greedy's algorithm is a process of getting from point a to point z by use of weighted edges. In this case, \(a = x_1\) and \(z = x_10\)

  • Start at \(x_1\text{.}\) From here we must choose the edge with the least weight to continue from \(x_1\text{.}\) \((x_1, x_2)\) has weight 2 and \((x_1, x_4)\) has weight 4. Since \(2 \lt 4\text{,}\) choose edge \((x_1, x_2)\) and continue on to \(x_2\text{.}\)

  • From \(x_2\) move on to \(x_5\) since the weight of \((x_2, x_5)\) is less than the weight of \((x_2, x_3)\text{.}\)

  • The weight of \((x_5, x_8)\) is less than the weight of \((x_5, x_6)\) so move on to \(x_8\text{.}\)

  • From \(x_8\) choose to move along \((x_8, x_6)\) since its weight is less than that of \((x_8, x_10)\text{.}\)

  • Since moving along \((x_6, x_5)\) would create a cycle, we are forced to travel along \((x_6, x_3)\) (which incidentally has the least weight anyway) since by definition of Greedy Path, we must "add the vertex q such that the edge (p,q) has the least weight of all edges from the current vertex p and it does not form a cycle."

  • Traveling along \((x_3, x_2)\) would also create a cycle, so move along \((x_3, x_4)\text{.}\)

  • From \(x_4\text{,}\) traveling to \(x_1\) would create a cycle, so we're forced to travel along \((x_4, x_7)\) to \(x_7\text{.}\)

  • From \(x_7\) the only option is to move along \((x_7, x_9)\) to \(x_9\text{.}\)

  • Our final step is traveling from \(x_9\) to \(x_10\) along \((x_9, x_10)\text{.}\)

Thus we create a path from \(x_1\) to \(x_10\) using the Greedy algorithm.

Problem 3.2.11.

Use Dijkstra's Algorithm to find the shortest path from \(x_1\) to \(x_{10}\) in FigureĀ 3.2.8.

Solution.

shortest path from \(X_1\) to \(X_{10}\)

\(P_6=(X_1,X_4,X_3,X_6,X_8,X_{10})\)

Problem 3.2.12.

Is the path produced by the Greedy algorithm as short as the path produced by Dijkstra's Algorithm? Comment.

Figure 3.2.13. Network for Shortest Paths

Problem 3.2.14.

Use Dijkstra's Algorithm to find the shortest path from \(x_1\) to \(x_3\) in FigureĀ 3.2.13.

Solution.

The steps of the algorithm are:

  1. Label \(x_1\) with \((-,0)\text{.}\)

  2. Let \(m=1\text{.}\) Label nothing.

  3. Let \(m=2\text{.}\) Label \(x_2\) with \((x_1,2)\text{.}\)

  4. Let \(m=3\text{.}\) Label \(x_4\) with \((x_4,3)\text{.}\)

  5. Let \(m=4\text{.}\) Label nothing.

  6. Let \(m=5\text{.}\) Label nothing.

  7. Let \(m=6\text{.}\) Label \(x_5\) with \((x_2, 6)\text{.}\)

  8. Let \(m=7\text{.}\) Label nothing.

  9. Let \(m=8\text{.}\) Label \(x_7\) with \((x_4,8)\text{.}\)

  10. Let \(m=9\text{.}\) Label \(x_8\) with \((x_5,9)\text{.}\)

  11. Let \(m=10\text{.}\) Label \(x_3\) with \((x_2,10)\text{.}\)

  12. Exit.

The shortest path from \(x_1\) to \(x_3\) is \((x_1,x_2,x_3)\) of weight 10.

Problem 3.2.15.

Use Dijkstra's Algorithm to find the shortest path from \(x_3\) to \(x_{10}\) in FigureĀ 3.2.13.

Problem 3.2.16.

Use Dijkstra's Algorithm to find the shortest path from \(x_1\) to \(x_{10}\) in FigureĀ 3.2.13.

Solution.

Using Dijkstra's algorithm, this is the shortest path from \(x_1 to x_10\) (highlighted in red).

Proof.

This is a proof by induction where the final step is proved by contradiction.

We would like to show that ItemĀ 3 always finds the shortest path between a starting vertex and an ending vertex in a network with non-negative edge weights. We will accomplish this by showing that the algorithm always finds the shortest path to the next vertex that the algorithm selects.

Let \(G\) be a connected network with non-negative edge weights. The algorithm begins by assigning the starting vertex a distance of zero from itself. Clearly no shorter path from the start to itself exists since all edge weights are non-negative so our base case is established.

Let us assume that the algorithm successfully finds the shortest path to some arbitrary vertex in \(G\text{.}\) We will call this vertex \(x\text{.}\) This is our induction hypothesis.

Now we would like to show that Djikstra's algorithm finds the shortest path to the next vertex that it selects. Call this vertex \(y\text{.}\) The algorithm will find that the distance from the start to \(y\) is:

\begin{equation} d(y)=d(x)+k(x,y)\tag{3.2.1} \end{equation}

Where \(d(x)\) is the shortest path to the prior vertex (which our induction hypothesis assumes the algorithm found) and \(k(x,y)\) is the edge weight between \(x\) and \(y\text{.}\)

Suppose this is not the shortest path to \(y\text{.}\) If that is the case, then the shortest path must pass through some vertex that has been hitherto unlabeled by the algorithm (else that other vertex would have just been our selection for \(x\)). That path must have some portion that the algorithm has labeled (even if it is just the starting vertex). Call the last vertex labeled by the algorithm on the true shortest path \(z\text{.}\) The true shortest path must then pass through a vertex that has not yet been labeled by the algorithm, call it \(p\text{,}\) then follows some path from \(p\) to \(y\text{,}\) call it \(py\text{.}\) It is important to note that \(d(py)\ge0\) since all of the edge weights are non-negative. Since the true shortest path is shorter than the path that is picked by the algorithm, the following must be true:

\begin{equation} d(z)+k(z,p)+d(py) \lt d(x)+k(x,y)\tag{3.2.2} \end{equation}

But that implies the following:

\begin{equation} d(z)+k(z,p) \lt d(x)+k(x,y)\tag{3.2.3} \end{equation}

Which leads us to a contradiction. The shortest path to \(z\) is known by our induction hypothesis and \(z\) is labeled by the algorithm. Thus, if a shorter path to \(y\) exists, then \(y\) is not the next vertex that is going to be labeled by the algorithm because \(z\) will be labeled before \(y\text{.}\) Thus, Djikstra's algorithm successfully find the shortest path to the next vertex that it labels.

Thus, since Djikstra's algorithm will always find the shortest path to the next vertex in a connected network with non-negative edge weights, it will find the shortest path between any two vertices provided that you select one of them as the starting vertex and allow the algorithm to run long enough to label the other. You are guaranteed that the algorithm will eventually select your ending vertex because the network is both connected and finite.

Definition 3.2.18. Spanning Tree.

A subgraph \(T\) of a graph \(G\) is a spanning tree if and only if it is a tree and every vertex of \(G\) is in \(T\text{.}\)

Problem 3.2.21.

Find a minimum spanning tree using Prim's Algorithm in FigureĀ 3.2.13.

Solution.

Begin by coloring the edges with the least weight.

  1. x_1x_2 has a weight of 2.

  2. x_3x_6 has a weight of 2.

  3. x_1x_4 has a weight of 3.

  4. x_5x_8 has a weight of 3.

  5. x_6x_8 has a weight of 3.

  6. x_10x_8 has a weight of 3.

Now we can connect the trees and the last two vertices.

  1. x_2x_5 has a weight of 4.

  2. x_10x_9 has a weight of 4.

  3. x_9x_7 has a weight of 4.

Thus a minimally weighted spanning tree is produced.

Problem 3.2.22.

Find a minimum spanning tree using Kruskal's Algorithm in FigureĀ 3.2.13.

Solution.

We begin with an empty set T. We choose x1x2 or x3x6 because they both have the least weight of 2.

Let's choose x1x2 add it to T. Then we look at edges incident with x1x2 and add the edge with the least weight to T.

We then look at edges incident to all edges in T, choose the edge with the least weight to add to T, and continue until every vertex is incident with an edge in T.

Below is the step-by-step building of T beginning with edge x1x2.

T = {x1x2}

T = {x1x2, x1x4}

T = {x1x4, x1x2, x2x5}

T = {x1x4, x1x2, x2x5, x5x8}

T = {x4x7, x1x4, X1x2, x2x5, x5x8}

T = {x7x9, x4x7, x1x4, X1x2, x2x5, x5x8}

T = {x9x10, x7x9, x4x7, x1x4, X1x2, x2x5, x5x8}

T = {x9x10, x7x9, x4x7, x1x4, X1x2, x2x5, x5x8, x8x6}

T = {x9x10, x7x9, x4x7, x1x4, X1x2, x2x5, x5x8, x8x6, x3x6}

Problem 3.2.24.

Is it possible to find a minimum spanning tree using Prim's Algorithm that is different from the previous one?

Solution.

Yes, generally. If a tree has edges of the same weight, you can create two distinct minimum spanning trees that are weighted the same.

Problem 3.2.25.

Conjecture conditions under which a minimal spanning tree would be unique.

Problem 3.2.26.

You and another employee are asked to find optimal networks. You find different solutions. How do you explain this to your boss?