0% found this document useful (0 votes)
14 views22 pages

DIJKSTRA'S ALGORITHM Edit

Dijkstra's Algorithm is used to find the shortest distance from a source vertex to all other vertices in a graph. It works by repeatedly selecting the nearest unvisited vertex and updating the distances to its neighboring vertices, but does not handle graphs with negative edges. The algorithm requires marking visited vertices and maintaining an overview of current shortest distances as it progresses through the graph.

Uploaded by

anashemasese11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

DIJKSTRA'S ALGORITHM Edit

Dijkstra's Algorithm is used to find the shortest distance from a source vertex to all other vertices in a graph. It works by repeatedly selecting the nearest unvisited vertex and updating the distances to its neighboring vertices, but does not handle graphs with negative edges. The algorithm requires marking visited vertices and maintaining an overview of current shortest distances as it progresses through the graph.

Uploaded by

anashemasese11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DIJKISTRA’S

ALGORITHM
• IT IS AN ALGORITHM TO FIND THE SHORTEST
DISTANCE FROM ONE VERTEX TO ALL
VERTICES

• It does this by repeatedly selecting the nearest


unvisited vertex and calculating the distances
to all unvisited neighboring vertices
• Used for solving single-source shortest
path problems for directed or undirected
paths
• NB: Single Source means that one
vertex is chosen to be the start and the
algorithm will find the shortest path from
the vertex to all other vertices.
• Does not work for graphs with negative
edges hence use the Bellman-Ford
Algorithm
To find the shortest path using the
DIJKSTRAS ALGORITHM you need to know

1. Which vertex is the source


2. It needs a way to mark vertices visited
3. It needs a way to mark vertices visited
4. It needs an overview of the current shortest distance to each
vertex as it works its way through graph, updating these
distances when a shorter distance is found
STEPS OF SOLVING DIJKSTRA’S ALGORITHM
1. Set initial distances for all vertices : 0 for the
source index and infinity for all other vertices
2. Choose the unvisited vertex with the shortest
distance from the start to be the current vertex
so the algorithm will always start with the
source as the current vertex
3.Choose the unvisited vertex with the shortest distance
from the start to be the current vertex so the algorithm
will always start with the source as the current vertex
4. Choose the unvisited vertex with the shortest distance
from the start to be the current vertex so the algorithm
will always start with the source as the current vertex
5. We are now done with current vertex so we
mark it as visited. A visited vertex is not
visited again
6. Go back to step 2 to choose a new current
vertex and keep repeating the steps until all
vertices are visited
• In the end we are left with the shortest path
from the source to vertex every other vertex
in the graph
EXAMPLE
 WE THEN LABEL THE VERTICES
D BEING THE SOURCE INDEX
ASSIGNED TO 0 AND THE REST
LABELLED TO INFINITY
 Vertex D is the current index
then it looks at the distances
of the adjacent vertices since
the initial distance to vertices
A and E is infinity ,the new
distances are then updated
with the edge
 After relaxing vertices A and E
vertex D is considered visited and
will not be visited again
 The next vertex to be chosen as
the current vertex is the one with
the shortest distance to the
source vertex (D)
 Among the previously visited
vertices Vertex E is therefore
chosen as the current vertex after
vertex D
 The distances to all adjacent and not
previously visisted vertices from vertex
E must be calculated and updated if
needed
 Then Calculate distances from
1. D to A via E =2+6=8, But the current
distance to vertex A is already 4 which
is lower so the distance is not updated
2. E to C =2+4=6
3. E TO G= 2+5=7
• Vetex A becomes the next vertex to be
visited since it has a lower distance from
D
 Distance from D to C via A
=4+3=7, which is higher than
the already set distance 6
 Vertex A is visited
 Current vertex is now vertex C
 Calculate distances :
1. D to B via C = 6+2=8, B
set to 8
2. D to F via C = 6+5=11, F
set to 11
3. D to G via C =6+5=11, 11
is higher than 7 hence G
remains 7
 C is visited
 G is the current index
 Calculate Distances D to F via G=
7+5=12, 12 is higher than the already
set value 11 hence Fremains 11
 G is visted
 Goes to vertex B
Calculates distance
D to F via B = 8
+2=10, Hence F is
set to 10
IMPLEMENTING OF DIJKSTRA USING PYTHON

• def dijkstra(self, start_vertex_data):


1. class Graph:
• start_vertex =
2. def __init__(self, size): self.vertex_data.index(start_vertex_data)

3. self.adj_matrix = [[0] * size for _ in range(size)] • distances = [float('inf')] * self.size

4. self.size = size • distances[start_vertex] = 0


• visited = [False] * self.size
5. self.vertex_data = [''] * size

• for _ in range(self.size):
6. def add_edge(self, u, v, weight):
• min_distance = float('inf')
7. if 0 <= u < self.size and 0 <= v < self.size: • u = None

8. self.adj_matrix[u][v] = weight • for i in range(self.size):

9. self.adj_matrix[v][u] = weight # For undirected • if not visited[i] and distances[i] < min_distance:
graph • min_distance = distances[i]
• u=i

10. def add_vertex_data(self, vertex, data):


• if u is None:
11. if 0 <= vertex < self.size:
• break
12. self.vertex_data[vertex] = data

• visited[u] = True
IMPLEMENTING OF DIJKSTRA USING PYTHON
• for v in range(self.size): • g.add_edge(3, 0, 4) # D - A, weight 5
• if self.adj_matrix[u][v] != 0 and not visited[v]: • g.add_edge(3, 4, 2) # D - E, weight 2
• alt = distances[u] + self.adj_matrix[u][v] • g.add_edge(0, 2, 3) # A - C, weight 3
• if alt < distances[v]: • g.add_edge(0, 4, 4) # A - E, weight 4

• distances[v] = alt • g.add_edge(4, 2, 4) # E - C, weight 4


• g.add_edge(4, 6, 5) # E - G, weight 5

• return distances • g.add_edge(2, 5, 5) # C - F, weight 5

• g = Graph(7) • g.add_edge(2, 1, 2) # C - B, weight 2


• g.add_edge(1, 5, 2) # B - F, weight 2
• g.add_edge(6, 5, 5) # G - F, weight 5
• g.add_vertex_data(0, 'A')

• g.add_vertex_data(1, 'B')
• # Dijkstra's algorithm from D to all vertices
• g.add_vertex_data(2, 'C')
• print("\nDijkstra's Algorithm starting from vertex D:")
• g.add_vertex_data(3, 'D')
• distances = g.dijkstra('D')
• g.add_vertex_data(4, 'E')
• for i, d in enumerate(distances):
• g.add_vertex_data(5, 'F')
• print(f"Distance from D to {g.vertex_data[i]}: {d}")
• g.add_vertex_data(6, 'G')

You might also like