Skip to content

an efficient algorithm for finding the shortest path from a source node to a destination node in a graph, with a visual representation

Notifications You must be signed in to change notification settings

Hadar933/Dijkstra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dijkstra

Description:

an efficient algorithm for finding the shortest path from source node to dest node in a graph. The run of the algorithm is portrayed via GUI interface that displays a minimum path from source node to destination node, and all the visited nodes


Main logic:

we run Dijkstra on a weighted graph, where each vertex is a "square" on the screen, an edge is a connection between two adjacent squared, and the weight is 1. as an example, consider a 3x3 pixel grid:

Capture

a graph G=<V,E>, that represents the grid, is comprised of the following edges and vertexes (starting from (0,0)):

V = ['(0,0)', '(0,1)', '(0,2)', '(1,0)', '(1,1)', '(1,2)', '(2,0)', '(2,1)', '(2,2)']

E = [('(0,0)', '(1,0)', 1), ('(0,0)', '(0,1)', 1), ('(0,1)', '(0,0)', 1),...,('(2,2)', '(2,1)', 1)]

where ('(0,0)', '(1,0)', 1), for example, means that node (0,0) is connected to node (1,0) with an edge weighing 1.

also:

  • the source and destination nodes are chosen by the user (two right clicks with the mouse)
  • the user can "draw" (left mouse click) on the screen pixels that represents a node that doesnt exist, and therefore the algorithm wont use in runtime (these can be thought of as barriers, and they are colored black)

Running the Visuals :

under Dijkstra.Main execute main()

example:

rsz_95909766-b6a68d00-0da7-11eb-8727-a042ad20cb2e

  • #f03c15 Minimum Path
  • #c5f015 Source Node
  • #1589F0 Destination Node
  • #000000 Blocked Nodes
  • #F18E14 Visited Nodes

complexity analysis

(not rigorously): Capture

Dijkstra:
source.set_dist_from_source(0)
Q = build_min_heap(graph)  # O(V)
while Q:  # O(V)
    u = heapq.heappop(Q)  # pops the min val (get value and remove from heap) O(logV)
    neighbors_of_u = graph.get_neighbors()[u.name]
    for v in neighbors_of_u:  # O(E)
        weight_u_v = graph.get_edge_weight(u.name, v.name)
        v_dist = v.dist_from_source
        u_dist = u.dist_from_source
        if v_dist > u_dist + weight_u_v:
            v.set_dist_from_source(u_dist + weight_u_v)
            heapq.heappush(Q, v)  # O(logV)

About

an efficient algorithm for finding the shortest path from a source node to a destination node in a graph, with a visual representation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages