Professional Documents
Culture Documents
This is the weighted graph for Dijkstra’s Algorithm along side the index for each lettered node.
This is the default Test case for all nodes reachable by S node. For both example 1 and 2.
Can see the printed value is the shortest path back to the starting node ‘w’ and that this formula works
with negative weights.
Starting from node B, due to the direction of the graph the only reachable nodes are A and C, which
have the values printed correctly. And as all other nodes are never reached their values are never
changed from their initial starting point of infinite.
Required modification was to print node paths instead of cost, was done by creating an array to then
append nodes onto, instead of overwriting a value held to the node.
This is the printed message to show there is a negative cycle and that the BF method exits out as values
cannot be achieved for some paths.
We can see here that when we have a graph that uses Bellman and doesn’t cycle we get the true
shortest path and the console prints the output.
As we can see the path to A from S is different, as the greedy method means it goes down the
first shortest path and marks A as sure as it cannot reach another node even though other nodes can
reach it. So when you then search down B it cannot search A as it is marked as complete despite being
able to have a shorter path.
Dynamic programming works very similarly to Divide and conquer. Breaking down a larger complex issue
into smaller, more simple sub-problems or tasks. Then you can optimise each sub problem by using the
best possible solution for each unique sub-problem and solve it that way. The main difference between
dynamic programming and the Divide and Conquer strategy is that D&C will solve every problem
individually, whereas DP has a memory in which it stores solutions, therefore if a solution that has
already been solved is seen in another sub-problem it just duplicates the solution instead of resolving it
to reduce complexity and runtime. This is also why DP cannot be used in quick sort or Binary search, as
with these Divide and Conquer methods the array is broken down recursively into smaller and smaller
pieces that do not overlap, as there is no overlapping problems DP is not applicable.
Dynamic Programming method to solve the climbing steps problem for any ‘n’. For every index I,
computes value of the ‘i’th position. Then prints the count as all the ways to reach the n step in step[n]
Case 1 to check n size under 3, where the loop starts, works on outputting correct pre-determined
lengths.
Case 2 to check that values outside of range do not produce result, behaving as intended.
Case 3 to check that results above 3 have correct number printed. Used 4 as can be worked out on
paper quickly to check method is right.
Case 4 used to check program continues to work for much larger examples and to look at how quickly
the number of possible paths grow.
This is the test case using the above weighted graph. The below image is of the Dijkstra algorithm from S
to all other nodes, as we can see it matches the above test case, and can assume that means the rest of
the rows are also correct. The graph also correctly outputs negative values as well. There is a lot of
infinite distances in this graph due to very few parts of it looping back around.