Skip to content

Commit 88a3383

Browse files
authored
Merge branch 'ivanmmarkovic:master' into master
2 parents 95b2786 + 7de13e7 commit 88a3383

File tree

26 files changed

+526
-107
lines changed

26 files changed

+526
-107
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/__pycache__/
2-
/venv/
2+
/venv/
3+
/.idea/

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ but it also contains other data structures, algorithms and problems.
5555
- [Palindrome](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/palindrome.py)
5656
- [Reverse linked list - recursive solution](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/reverse-linked-list.py)
5757
- [Reverse linked list - iterative solution](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/reverse-linked-list-iterative.py)
58+
- [Reverse linked list - iterative solution stack](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/reverse-linked-list-iterative-stack.py)
5859
- [Reverse list](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/reverse-list.py)
5960
- [Reverse string](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/reverse-string.py)
6061
- [Sum numbers - binary recursion](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/recursion/sum-numbers-binary-recursion.py)
@@ -108,11 +109,20 @@ but it also contains other data structures, algorithms and problems.
108109
- [Dijkstra algorithm - Shortest path](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/dijkstra)
109110
- [Priority Queue implementation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/dijkstra/priority-queue-impl-adjacency-map)
110111
- [Matrix implementation](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/dijkstra/matrix-impl)
112+
- [Bellman-Ford algorithm](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/bellman-ford)
113+
- [Bellman-Ford algorithm - negative weight cycle](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/bellman-ford-negative-weight-cycle)
111114
- [Minimum Spanning Tree](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree)
112115
- [Minimum Spanning Tree in undirected graph](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree/breadth-first-search)
113116
- [Prim's algorithm - Minimum Spanning Tree in undirected weighted graph](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree/prims-algorithm)
117+
- [Kruskal's algorithm - Minimum Spanning Tree in undirected weighted graph](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/minimum-spanning-tree/kruskals-algorithm)
114118
- [Is graph bipartite](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/is-graph-bipartite)
115-
- [Union find](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/union-find-path-compression)
116-
119+
- [Union find](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/union-find)
120+
- [Number of connected components](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/union-find/number-of-connected-components)
121+
- [Union find path compression](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/union-find/union-find-path-compression)
122+
- [Kosaraju's algorithm](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/graphs/kosarajus-algorithm)
117123
### [Trie](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/trie)
118124
- [Trie](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/trie)
125+
126+
127+
### [Substring search](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/tree/master/substring-search)
128+
- [Brute force algorithm](https://github.com/ivanmmarkovic/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python/blob/master/substring-search/brute_force.py)

graphs/bellman-ford-negative-weight-cycle/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/bellman-ford-negative-weight-cycle/.idea/Bellman–Ford.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/bellman-ford-negative-weight-cycle/.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/bellman-ford-negative-weight-cycle/.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/bellman-ford-negative-weight-cycle/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
# Bellman-Ford algorithm
3+
# Find shortest paths from one vertex,
4+
# to all other vertices in weighted graph.
5+
# Runtime O(V*E)
6+
7+
# Negative weight cycle will be found with one more loop through edges
8+
# If there is a need to update distance than it is a negative weight cycle
9+
10+
class Graph:
11+
def __init__(self):
12+
self.vertices: list = []
13+
self.edges: list = []
14+
self.distance: dict = {}
15+
self.prev: dict = {}
16+
17+
def add_vertex(self, label: str):
18+
self.vertices.append(label)
19+
self.distance[label] = None
20+
self.prev[label] = None
21+
22+
def add_edge(self, label1: str, label2: str, weight: int):
23+
self.edges.append([label1, label2, weight])
24+
25+
def bellman_ford(self, source: str):
26+
self.distance[source] = 0
27+
28+
for _ in range(len(self.vertices)):
29+
30+
for edge in self.edges:
31+
label1: str = edge[0]
32+
label2: str = edge[1]
33+
weight: int = edge[2]
34+
35+
if self.distance[label1] is None:
36+
continue
37+
if self.distance[label2] is None:
38+
self.distance[label2] = self.distance[label1] + weight
39+
self.prev[label2] = label1
40+
continue
41+
if self.distance[label1] + weight < self.distance[label2]:
42+
self.distance[label2] = self.distance[label1] + weight
43+
self.prev[label2] = label1
44+
continue
45+
46+
for edge in self.edges:
47+
label1: str = edge[0]
48+
label2: str = edge[1]
49+
weight: int = edge[2]
50+
51+
if self.distance[label1] is None:
52+
continue
53+
if self.distance[label2] is None:
54+
continue
55+
if self.distance[label1] + weight < self.distance[label2]:
56+
print(f'Negative weight cycle from {label1} to {label2}')
57+
58+
def print_distances(self, source: str):
59+
for v in self.vertices:
60+
if v != source:
61+
distance: int = self.distance[v]
62+
print(f'Distance from {source} to {v} is {distance}')

graphs/bellman-ford/graph.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# Bellman-Ford algorithm
3+
# Find shortest paths from one vertex,
4+
# to all other vertices in weighted graph.
5+
# Runtime O(V*E)
6+
7+
class Graph:
8+
def __init__(self):
9+
self.vertices: list = []
10+
self.edges: list = []
11+
self.distance: dict = {}
12+
self.prev: dict = {}
13+
14+
def add_vertex(self, label: str):
15+
self.vertices.append(label)
16+
self.distance[label] = None
17+
self.prev[label] = None
18+
19+
def add_edge(self, label1: str, label2: str, weight: int):
20+
self.edges.append([label1, label2, weight])
21+
22+
def bellman_ford(self, source: str):
23+
self.distance[source] = 0
24+
25+
for _ in range(len(self.vertices)):
26+
27+
for edge in self.edges:
28+
label1: str = edge[0]
29+
label2: str = edge[1]
30+
weight: int = edge[2]
31+
32+
if self.distance[label1] is None:
33+
continue
34+
if self.distance[label2] is None:
35+
self.distance[label2] = self.distance[label1] + weight
36+
self.prev[label2] = label1
37+
continue
38+
if self.distance[label1] + weight < self.distance[label2]:
39+
self.distance[label2] = self.distance[label1] + weight
40+
self.prev[label2] = label1
41+
continue
42+
43+
def print_distances(self, source: str):
44+
for v in self.vertices:
45+
if v != source:
46+
distance: int = self.distance[v]
47+
print(f'Distance from {source} to {v} is {distance}')

graphs/is-graph-bipartite/graph.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@
33

44
class Graph:
55
def __init__(self):
6-
self._vertices: list = []
7-
self._colors: dict = {}
8-
self._adjacency_matrix: dict = {}
9-
10-
def add_vertex(self, label: str):
11-
self._vertices.append(label)
12-
self._colors[label] = None
13-
self._adjacency_matrix[label]: list = []
14-
15-
def add_edge(self, label1: str, label2: str):
16-
self._adjacency_matrix[label1].append(label2)
17-
self._adjacency_matrix[label2].append(label1)
18-
19-
def bipartite_check(self) -> bool:
20-
for vertex in self._vertices:
21-
if self._colors[vertex] is not None:
6+
self.vertices: list = []
7+
self.adjacency_list: dict = {}
8+
self.color: dict = {}
9+
10+
def add_vertex(self, label: str = None):
11+
self.vertices.append(label)
12+
self.adjacency_list[label]: list = []
13+
self.color[label] = None
14+
15+
def add_edge(self, label1: str = None, label2: str = None):
16+
self.adjacency_list[label1].append(label2)
17+
self.adjacency_list[label2].append(label1)
18+
19+
def is_bipartite(self) -> bool:
20+
for vertex in self.vertices:
21+
if self.color[vertex] is not None:
2222
continue
23-
self._colors[vertex] = "red"
2423
q: Queue = Queue()
24+
self.color[vertex] = "red"
2525
q.enqueue(vertex)
2626
while not q.is_empty():
27-
v = q.dequeue()
28-
for neighbour in self._adjacency_matrix[v]:
29-
if self._colors[neighbour] == self._colors[v]:
27+
tmp: str = q.dequeue()
28+
for neighbour in self.adjacency_list[tmp]:
29+
if self.color[neighbour] == self.color[tmp]:
3030
return False
31-
if self._colors[neighbour] is None:
32-
if self._colors[v] == "red":
33-
self._colors[neighbour] = "blue"
31+
if self.color[neighbour] is None:
32+
if self.color[tmp] == "red":
33+
self.color[neighbour] = "blue"
3434
else:
35-
self._colors[neighbour] = "red"
35+
self.color[neighbour] = "red"
3636
q.enqueue(neighbour)
3737
return True
3838

3939

4040

4141

4242

43+

0 commit comments

Comments
 (0)