Skip to content

Commit 09a84e4

Browse files
authored
Merge pull request #247 from utkarshrai2811/dijkstra-algo-py
Dijkstra algo py
2 parents 445a153 + f9e8a89 commit 09a84e4

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

Algorithms/dijkstra_algo.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Library for INT_MAX
2+
import sys
3+
4+
class Graph():
5+
6+
def __init__(self, vertices):
7+
self.V = vertices
8+
self.graph = [[0 for column in range(vertices)]
9+
for row in range(vertices)]
10+
11+
def printSolution(self, dist):
12+
print ("Vertex tDistance from Source")
13+
for node in range(self.V):
14+
print (node, "t", dist[node])
15+
16+
# A utility function to find the vertex with
17+
# minimum distance value, from the set of vertices
18+
# not yet included in shortest path tree
19+
def minDistance(self, dist, sptSet):
20+
21+
# Initilaize minimum distance for next node
22+
min = sys.maxsize
23+
24+
# Search not nearest vertex not in the
25+
# shortest path tree
26+
for v in range(self.V):
27+
if dist[v] < min and sptSet[v] == False:
28+
min = dist[v]
29+
min_index = v
30+
31+
return min_index
32+
33+
# Funtion that implements Dijkstra's single source
34+
# shortest path algorithm for a graph represented
35+
# using adjacency matrix representation
36+
def dijkstra(self, src):
37+
38+
dist = [sys.maxsize] * self.V
39+
dist[src] = 0
40+
sptSet = [False] * self.V
41+
42+
for cout in range(self.V):
43+
44+
# Pick the minimum distance vertex from
45+
# the set of vertices not yet processed.
46+
# u is always equal to src in first iteration
47+
u = self.minDistance(dist, sptSet)
48+
49+
# Put the minimum distance vertex in the
50+
# shotest path tree
51+
sptSet[u] = True
52+
53+
# Update dist value of the adjacent vertices
54+
# of the picked vertex only if the current
55+
# distance is greater than new distance and
56+
# the vertex in not in the shotest path tree
57+
for v in range(self.V):
58+
if self.graph[u][v] > 0 and \
59+
sptSet[v] == False and \
60+
dist[v] > dist[u] + self.graph[u][v]:
61+
dist[v] = dist[u] + self.graph[u][v]
62+
63+
self.printSolution(dist)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
7171
<td align="center"><a href="https://github.com/codermind-divya"><img src="https://avatars0.githubusercontent.com/u/49811570?v=4" width="100px;" alt=""/><br /><sub><b>Divya Jain</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=codermind-divya" title="Code">💻</a></td>
7272
<td align="center"><a href="https://github.com/mnb27"><img src="https://avatars1.githubusercontent.com/u/52792904?v=4" width="100px;" alt=""/><br /><sub><b>Aman Bilaiya</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=mnb27" title="Code">💻</a> <a href="#ideas-mnb27" title="Ideas, Planning, & Feedback">🤔</a></td>
7373
<td align="center"><a href="https://www.linkedin.com/in/brijesh-kumar-887b05174"><img src="https://avatars3.githubusercontent.com/u/36602697?v=4" width="100px;" alt=""/><br /><sub><b>BRIJESH KUMAR</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=brijeshsos66" title="Code">💻</a> <a href="#design-brijeshsos66" title="Design">🎨</a></td>
74+
<td align="center"><a href="https://github.com/utkarshrai2811/"><img src="https://github.com/utkarshrai2811.png" width="100px;" alt=""/><br /><sub><b>Utkarsh Rai</b></sub></a><br /><a href="#design-utkarshrai2811" title="Design">🎨</a> <a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=utkarshrai2811" title="Code">💻</a></td>
7475
</tr>
7576
</table>
7677

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Insertion sort.
2+
3+
pub fn insertion_sort(arr: &mut [i32]) {
4+
for i in 1..arr.len() {
5+
let mut j = i;
6+
while j > 0 && arr[j - 1] > arr[j] {
7+
arr.swap(j - 1, j);
8+
j -= 1;
9+
}
10+
}
11+
}
12+
13+
/// Binary insertion sort.
14+
15+
/// Binary insertion sort is a insertion sort variant that utilizes binary
16+
17+
/// search to reduce comparisons in a normal insertion sort.
18+
19+
pub fn binary_insertion_sort(arr: &mut [i32]) {
20+
for i in 1..arr.len() {
21+
let val = arr[i];
22+
let mut j = i;
23+
let pos = arr[..i].binary_search(&val).unwrap_or_else(|pos| pos);
24+
// Swap all elements until specific position.
25+
while j > pos {
26+
arr.swap(j - 1, j);
27+
j -= 1;
28+
}
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
34+
mod base {
35+
use super::*;
36+
base_cases!(insertion_sort);
37+
}
38+
39+
#[cfg(test)]
40+
41+
mod binary_insertion {
42+
use super::*;
43+
base_cases!(binary_insertion_sort);
44+
}

0 commit comments

Comments
 (0)