- 0-Introduction
- 1-Chatbot Variables
- 2-Password IF Statements
- 3-Create Main Menu functions
- 4-Complete Quiz 3 Questions
- 5-Adding Score variable global
- 6-Debug this code
- 7-Introducing While loops Boolean flags
- 8-Introducing Validation password creation
- 9a-For Loop Guess the number
- 9b-For Loop Password Challenge
- 9c-For Loop Times table challenge
- FINAL CHALLENGE
- Skills Consolidation Task 1
- Test 1
- 0-Introduction
- 1-Introducing Lists
- 2-Personality Predictor Lists app
- 3-Players assigned weapons lists
- 3a-Nested List Matrix Snakes and Ladders game
- 3b Nested List Super Extension Complete game
- 4-String Manipulation Username Creator
- 5-StringManipulation Email Creator based on Validated username
- 6-Strip characters from password app
- 7-Create registration feature using lists
- 8-Introducing Dictionaries registration with dicts
- 8a-Course teacher finder program with dicts
- 9a-Football Club app Create and Learn
- 9b-Online Shopping Basket Checkout Program Create and Learn
- Test 1
- 0-Introduction
- 1-Introducing File Handling
- 2-READ from fake facebook file
- 3-SEARCH for username return no of friends
- 4-SEARCH by ID return full record listing
- 5-ADD WRITE a new user to file
- 6-SORT file by USER ID and Last Name
- 7-Bingo game store scores
- 8-Modulo Magic Program
- 9-Create Maths Quiz Program Tutorial
- 9a-How-to-DELETE user record row from file
- 9b-How to EDIT a field in a file
- Test 1

- 00 Intro
- 01 Main Menu Start Screen
- 02 Registration Feature Part1
- 03 Secure Password Creator
- 04 Login Functionality
- 05 MainFilms Menu Members
- 06 Allow Users to View films
- 07 Store Viewed Films by user
- 08 Allow users to like films
- 09 Search by Title
- 10 Search by Rating
- 11 Recommendations based on viewing FinalSolutions

- 00 Overview Start Here
- 01 Connect Create table
- 02 Add records to table
- 03 Fetch Display records
- 04 Update database records
- 05 Delete records
- 06 Search by condition where clause
- 07 Search for key phrase word
- 08 Sorting in SQLite
- 09 Search return selected fields
- 10 Count no rows
- 11 Find Max Value in column
- 12 Calculate Average
- 13 Calculate SUM total
- 14 Login username password sqlite

- 00-Introduction to OOP and Classes
- 01-Setup Game Canvas
- 02-Create a Ball Class
- 03-Setup main animation loop
- 04-Make the ball move up
- 05-Create bouncing ball movement
- 06-Change Starting Direction
- 07-Right left wall collision detection
- 08-Add Pong bat paddle class
- 09-Bat movement
- 10 Bat Ball collision detection
- 11 End Game Feature if ball hits bottom
- 12 Display text game over

**Dijkstra's algorithm** is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1958 and published three years later. The algorithm exists in many variants; Dijkstra's original variant found the shortest path between two nodes, but a more common variant fixes a single node as the "source" node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree

#1 Create your own code to illustrate the functionality of this algorithm

#2 Comment each line of the python solution below to show your understanding of the algorithm

nodes = ('A', 'B', 'C', 'D', 'E', 'F', 'G') distances = { 'B': {'A': 5, 'D': 1, 'G': 2}, 'A': {'B': 5, 'D': 3, 'E': 12, 'F' :5}, 'D': {'B': 1, 'G': 1, 'E': 1, 'A': 3}, 'G': {'B': 2, 'D': 1, 'C': 2}, 'C': {'G': 2, 'E': 1, 'F': 16}, 'E': {'A': 12, 'D': 1, 'C': 1, 'F': 2}, 'F': {'A': 5, 'E': 2, 'C': 16}} unvisited = {node: None for node in nodes} #using None as +inf visited = {} current = 'B' currentDistance = 0 unvisited[current] = currentDistance while True: for neighbour, distance in distances[current].items(): if neighbour not in unvisited: continue newDistance = currentDistance + distance if unvisited[neighbour] is None or unvisited[neighbour] > newDistance: unvisited[neighbour] = newDistance visited[current] = currentDistance del unvisited[current] if not unvisited: break candidates = [node for node in unvisited.items() if node[1]] current, currentDistance = sorted(candidates, key = lambda x: x[1])[0] print(visited)

For a given source node in the graph, the algorithm finds the shortest path between that node and every other. It can also be used for finding the shortest paths from a single node to a single destination node by stopping the algorithm once the shortest path to the destination node has been determined. For example, if the nodes of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path algorithm is widely used in network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and Open Shortest Path First (OSPF). It is also employed as a subroutine in other algorithms such as Johnson's.

#1: Dijkstra's algorithm to find the shortest path between *a* and *b*. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors.

#2: Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a goal node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are visited ones, with color representing the distance: the greener, the farther. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic identically equal to 0.

#2: A demo of Dijkstra's algorithm based on Euclidean distance. Red lines are the shortest path covering, i.e., connecting *u* and prev[*u*]. Blue lines indicate where relaxing happens, i.e., connecting *v* with a node *u* in *Q*, which gives a shorter path from the source to *v*.

In the following algorithm, the code u ← vertex in *Q* with min dist[u], searches for the vertex `u` in the vertex set `Q` that has the least dist[`u`] value. length(`u`, `v`) returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes `u` and `v`. The variable `alt` on line 17 is the length of the path from the root node to the neighbor node `v` if it were to go through `u`. If this path is shorter than the current shortest path recorded for `v`, that current path is replaced with this `alt` path. The prev array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to the source.

1functionDijkstra(Graph,source): 2 3 create vertex set Q 4 5for eachvertexvinGraph:// Initialization6 dist[v] ← INFINITY// Unknown distance from source to v7 prev[v] ← UNDEFINED// Previous node in optimal path from source8 addvtoQ// All nodes initially in Q (unvisited nodes)9 10 dist[source] ← 0// Distance from source to source11 12whileQis not empty: 13u← vertex inQwith min dist[u]// Node with the least distance will be selected first14 removeufromQ15 16for eachneighborvofu:// where v is still in Q.17alt← dist[u] + length(u,v) 18ifalt< dist[v]:// A shorter path to v has been found19 dist[v] ←alt20 prev[v] ←u21 22returndist[], prev[]

The following two examples have been taken from: algocoding.wordpress.com

# Dijktra's shortest path algorithm. Prints the path from source to target. def dijkstra(adj, source, target): INF = ((1<<63) - 1)//2 pred = { x:x for x in adj } dist = { x:INF for x in adj } dist[source] = 0 PQ = [] heapq.heappush(PQ, [dist[source], source]) while(PQ): u = heapq.heappop(PQ) # u is a tuple [u_dist, u_id] u_dist = u[0] u_id = u[1] if u_dist == dist[u_id]: #if u_id == target: # break for v in adj[u_id]: v_id = v[0] w_uv = v[1] if dist[u_id] + w_uv < dist[v_id]: dist[v_id] = dist[u_id] + w_uv heapq.heappush(PQ, [dist[v_id], v_id]) pred[v_id] = u_id if dist[target]==INF: stdout.write("There is no path between " + source + " and " + target) else: st = [] node = target while(True): st.append(str(node)) if(node==pred[node]): break node = pred[node] path = st[::-1] stdout.write("The shortest path is: " + " ".join(path))

- Step 0 (initialization): Assign to all nodes a distance value of ‘inf’, except for the source node which has a distance of zero. Initialize an empty set S. Initialize a predecessor dictionary.
- Repeat loop:
- Step 1: add to S the node with the smallest distance (among the nodes that touch S via an edge). Call that node u.
- Step 2: for u consider all new outgoing edges {u, v1}, {u, v2}, …, {u, vk} (i.e. edges that lead to nodes which are not yet in S). Check if these edges lead to a smaller distance value for the nodes v1, v2, …, vk. If it does for vj, update the distance for vj to the smaller value and set the predecessor of vj to u.
- If there are no nodes left that touch S via an edge, then break the loop.
- Lines 5 to 9 show the initialization. INF is initialized to half of the maximum value of a signed 64 bit integer (a signed 64 bit integer can display integers from to .
- In lines 9 and 10 we initialize a priority queue and insert the tuple (dist[source], source) since we start with the source node.
- Lines 12 to 25 implement the repeating loop. In line 13 we try to find the next node with the smallest distance value. We do this with a priority queue. In line 16 we discard “wrong tuples”, see also Part 3a.
- Lines 17 and 18 are optional. They make the algorithm stop once the minimum distance for the target node has been found. Note though that if you uncomment these lines the distances to other nodes beside the target node are not necessarily found, so uncomment these lines only if you are interested in a particular target node.
- In line 27 we check whether a path has been found.
- In lines 30 to 38 we reconstruct the shortest path with the help of the predecessor dictionary.
- In particular this is done in lines 32 to 36 where we follow the shortest path backwards from the target to the source. We recognize the source by checking if
`pred[node] == node`

, see also the way we initialized the predecessor dictionary in line 6.

**Notes:**

– Dijkstra’s algorithm only works if the edge weights are positive.

– We’ve used a priority queue that does not support a decrease-key operation as described in Part 3a. This differs from the description in the literature where a priority queue with a decrease-key operation is used.

– You have probably noticed that in our implementation we did not use a set S that contains the nodes for which we have found the minimum distance. The priority queue holds those nodes that “touch” our set S via an edge, see also part 1. When a node gets extracted from the priority queue it means we are adding it to our set S.

– Line 22 automatically ignores edges that go to nodes v within S because `dist[u_id] + w_uv < dist[v_id]`

can never be true due to the fact that `dist[v_id]`

is the minimum distance for v once it has been added to S.

import heapq from sys import stdin, stdout # Dijktra's shortest path algorithm. Prints the path from source to target. def dijkstra(adj, source, target): INF = ((1<<63) - 1)//2 pred = { x:x for x in adj } dist = { x:INF for x in adj } dist[ source ] = 0 PQ = [] heapq.heappush(PQ, [dist[ source ], source]) while(PQ): u = heapq.heappop(PQ) # u is a tuple [u_dist, u_id] u_dist = u[0] u_id = u[1] if u_dist == dist[u_id]: #if u_id == target: # break for v in adj[u_id]: v_id = v[0] w_uv = v[1] if dist[u_id] + w_uv < dist[v_id]: dist[v_id] = dist[u_id] + w_uv heapq.heappush(PQ, [dist[v_id], v_id]) pred[v_id] = u_id if dist[target]==INF: stdout.write("There is no path between ", source, "and", target) else: st = [] node = target while(True): st.append(str(node)) if(node==pred[node]): break node = pred[node] path = st[::-1] stdout.write("The shortest path is: " + " ".join(path) + "\n\n") stdout.write("The distance from 'a' to 'i' is: " + str(dist['i']) + "\n\n") stdout.write("distance dictionary: " + str(dist) + "\n\n") stdout.write("predecessor dictionary: " + str(pred)) #---------------------------------------------------------- def main(): adj = {'c': [('b', 0.32), ('e', 0.17), ('f', 0.91)], 'g': [('d', 0.17), ('e', 0.27), ('h', 0.92)], 'i': [('e', 1.98), ('f', 0.13), ('h', 0.22)], 'f': [('c', 0.91), ('e', 0.33), ('i', 0.13)], 'h': [('e', 0.18), ('g', 0.92), ('i', 0.22)], 'd': [('a', 0.72), ('e', 0.29), ('g', 0.17)], 'a': [('b', 0.95), ('d', 0.72), ('e', 1.75)], 'e': [('a', 1.75), ('b', 0.82), ('c', 0.17), ('d', 0.29), ('f', 0.33), ('g', 0.27), ('h', 0.18), ('i', 1.98)], 'b': [('a', 0.95), ('c', 0.32), ('e', 0.82)]} dijkstra(adj, 'a', 'i') #---------------------------------------------------------- if __name__ == "__main__": main()

Dijkstra thought about the shortest path problem when working at the Mathematical Center in Amsterdam in 1956 as a programmer to demonstrate the capabilities of a new computer called ARMAC.His objective was to choose both a problem as well as an answer (that would be produced by computer) that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (64, so that 6 bits would be sufficient to encode the city number). A year later, he came across another problem from hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the back panel of the machine. As a solution, he re-discovered the algorithm known as Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim). Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.

Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a goal node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are visited ones, with color representing the distance: the greener, the farther. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic identically equal to 0.

Let the node at which we are starting be called the **initial node**. Let the **distance of node Y** be the distance from the

- Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.
- Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the
*unvisited set*. - For the current node, consider all of its neighbors and calculate their
*tentative*distances. Compare the newly calculated*tentative*distance to the current assigned value and assign the smaller one. For example, if the current node*A*is marked with a distance of 6, and the edge connecting it with a neighbor*B*has length 2, then the distance to*B*(through*A*) will be 6 + 2 = 8. If B was previously marked with a distance greater than 8 then change it to 8. Otherwise, keep the current value. - When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the
*unvisited set*. A visited node will never be checked again. - If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the
*unvisited set*is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished. - Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the new "current node", and go back to step 3.

**Note:**For ease of understanding, this discussion uses the terms**intersection**,**road**and**map**— however, in formal terminology these terms are**vertex**,**edge**and**graph**, respectively.

Suppose you would like to find the *shortest path* between two intersections on a city map: a *starting point* and a *destination*. Dijkstra's algorithm initially marks the distance (from the starting point) to every other intersection on the map with *infinity*. This is done not to imply there is an infinite distance, but to note that those intersections have not yet been visited; some variants of this method simply leave the intersections' distances *unlabeled*. Now, at each iteration, select the *current intersection*. For the first iteration, the current intersection will be the starting point, and the distance to it (the intersection's label) will be *zero*. For subsequent iterations (after the first), the current intersection will be a *closest unvisited intersection* to the starting point (this will be easy to find).

From the current intersection, *update* the distance to every unvisited intersection that is directly connected to it. This is done by determining the *sum* of the distance between an unvisited intersection and the value of the current intersection, and relabeling the unvisited intersection with this value (the sum), if it is less than its current value. In effect, the intersection is relabeled if the path to it through the current intersection is shorter than the previously known paths. To facilitate shortest path identification, in pencil, mark the road with an arrow pointing to the relabeled intersection if you label/relabel it, and erase all others pointing to it. After you have updated the distances to each neighboring intersection, mark the current intersection as *visited*, and select an unvisited intersection with minimal distance (from the starting point) – or the lowest label—as the current intersection. Nodes marked as visited are labeled with the shortest path from the starting point to it and will not be revisited or returned to.

Continue this process of updating the neighboring intersections with the shortest distances, then marking the current intersection as visited and moving onto a closest unvisited intersection until you have marked the destination as visited. Once you have marked the destination as visited (as is the case with any visited intersection) you have determined the shortest path to it, from the starting point, and can *trace your way back, following the arrows in reverse*; in the algorithm's implementations, this is usually done (after the algorithm has reached the destination node) by following the nodes' parents from the destination node up to the starting node; that's why we also keep track of each node's parent.

This algorithm makes no attempt to direct "exploration" towards the destination as one might expect. Rather, the sole consideration in determining the next "current" intersection is its distance from the starting point. This algorithm therefore expands outward from the starting point, interactively considering every node that is closer in terms of shortest path distance until it reaches the destination. When understood in this way, it is clear how the algorithm necessarily finds the shortest path. However, it may also reveal one of the algorithm's weaknesses: its relative slowness in some topologies.