# Python program for Dijkstra's single source shortest
# path algorithm. The program is for adjacency matrix
# representation of the graph
# http://www.geeksforgeeks.org/printing-paths-dijkstras-shortest-path-algorithm/
#
#
from collections import defaultdict
#Class to represent a graph
class Graph:
# A utility function to find the vertex with minimum dist value, from
# the set of vertices still in queue
def minDistance(self,dist,queue):
# Initialize min value and min_index as -1
minimum = float("Inf")
min_index = -1
#from the dist array,pick one which has min value and is till in queue
for i in range(len(dist)):
if dist[i] < minimum and i in queue:
minimum = dist[i]
min_index = i
return min_index
# Function to print shortest path from source to j
# using parent array
def printPath(self, parent, j):
if parent[j] == -1 : #Base Case : If j is source
print j,
return
self.printPath(parent , parent[j])
print j,
# A utility function to print the constructed distance
# array
def printSolution(self, dist, parent):
src = 0
print("Vertex \t\tDistance from Source\tPath")
for i in range(1, len(dist)):
print("\n%d --> %d \t\t%d \t\t\t\t\t" % (src, i, dist[i])),
self.printPath(parent,i)
'''Function that implements Dijkstra's single source shortest path
algorithm for a graph represented using adjacency matrix
representation'''
def dijkstra(self, graph, src):
row = len(graph)
col = len(graph[0])
# The output array. dist[i] will hold the shortest distance from src to i
# Initialize all distances as INFINITE
dist = [float("Inf")] * row
#Parent array to store shortest path tree
parent = [-1] * row
# Distance of source vertex from itself is always 0
dist[src] = 0
# Add all vertices in queue
queue = []
for i in range(row):
queue.append(i)
#Find shortest path for all vertices
while queue:
# Pick the minimum dist vertex from the set of vertices
# still in queue
u = self.minDistance(dist,queue)
# remove min element
queue.remove(u)
# Update dist value and parent index of the adjacent vertices of
# the picked vertex. Consider only those vertices which are still in
# queue
for i in range(col):
'''Update dist[i] only if it is in queue, there is
an edge from u to i, and total weight of path from
src to i through u is smaller than current value of
dist[i]'''
if graph[u][i] and i in queue:
if dist[u] + graph[u][i] < dist[i]:
dist[i] = dist[u] + graph[u][i]
parent[i] = u
# print the constructed distance array
self.printSolution(dist,parent)
g= Graph()
graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0, 11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9, 14, 0, 0, 0],
[0, 0, 0, 9, 0, 10, 0, 0, 0],
[0, 0, 4, 14, 10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8, 11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
]
#Print the solution
g.dijkstra(graph,0)
#This code is contributed by Neelam Yadav
About Online Python Compiler
Try our Online Python Compiler (Version Python v2.7.13) to Edit, Run, and Share your Python Code directly from your browser. This online development environment provides you the latest version Python v2.7.13.
How to use Online Python Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Python Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Python Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Python Compiler: Save and Share Python Code (Project)
Save Python Project Online
You can save your Python Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Python Project Online
You can use this feature to share your Python Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Python Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Python Compiler
There are several benefits of using the Online Python Compiler to run your Python code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.