Skip to content

Commit 916ecde

Browse files
Added Graph
1 parent de973e3 commit 916ecde

14 files changed

+309
-21
lines changed

.idea/workspace.xml

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

DataStructures/Graph/Graph.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from DataStructures.LinkedList import LinkedList
2+
3+
4+
class Graph:
5+
def __init__(self, vertices, is_directed=True):
6+
self.is_directed = is_directed
7+
# Total number of vertices in the Graph
8+
self.vertices = vertices
9+
# Dictionary that holds the adjacency list for all the vertices
10+
self.adjacency_list = {}
11+
12+
def _add_edge_helper(self, source, destination):
13+
if source in self.adjacency_list:
14+
adjacency_list_of_source = self.adjacency_list[source]
15+
else:
16+
adjacency_list_of_source = LinkedList()
17+
adjacency_list_of_source.insert(destination, 0)
18+
self.adjacency_list[source] = adjacency_list_of_source
19+
20+
def add_edge(self, source, destination):
21+
if self.is_directed is False:
22+
self._add_edge_helper(destination, source)
23+
24+
self._add_edge_helper(source, destination)
25+
26+
def add_edges(self, list_of_edges):
27+
for edge in list_of_edges:
28+
self.add_edge(edge[0], edge[1])
29+
30+
def print_graph(self):
31+
for key in self.adjacency_list:
32+
adjacency_list_of_key = self.adjacency_list[key]
33+
print('{0} => '.format(key), end='')
34+
adjacency_list_of_key.print_list()
35+
36+

DataStructures/Graph/graph_driver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from DataStructures.Graph import Graph
2+
3+
directed_graph_1 = Graph(4, True)
4+
directed_graph_1.add_edges([[0, 1], [0, 1], [0, 2], [1, 3], [2, 3]])
5+
directed_graph_1.print_graph()
2.51 KB
Binary file not shown.

Leetcode/add_two_numbers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def add_two_numbers(l1: Node, l2: Node) -> Node:
6060

6161
head = add_two_numbers(l1.head, l2.head)
6262
l3 = LinkedList()
63-
l3.print_list(head)
63+
l3.print_list(head)

Leetcode/find_minimum.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import math
2+
3+
4+
def find_minimum(arr):
5+
"""
6+
Time Complexity: O(n)
7+
Space Complexity: O(1)
8+
"""
9+
min_value = math.inf
10+
11+
for x in arr:
12+
if x < min_value:
13+
min_value = x
14+
15+
return min_value
16+
17+
18+
print(find_minimum([10, 2, 5, 6, 8]))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import OrderedDict
2+
3+
4+
def first_non_repeating_integer(arr):
5+
"""
6+
Time Complexity: O(n)
7+
Space Complexity: O(m)
8+
"""
9+
count_map = OrderedDict()
10+
11+
for x in arr:
12+
if x in count_map:
13+
count_map[x] = count_map[x]+1
14+
else:
15+
count_map[x] = 1
16+
17+
for key, value in count_map.items():
18+
if count_map[key] == 1:
19+
return key
20+
21+
return -1
22+
23+
24+
print(first_non_repeating_integer([9, 2, 3, 2, 6, 6]))
25+
print(first_non_repeating_integer([4, 5, 1, 2, 0, 4]))

Leetcode/merge_sorted_arrays.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def merge_sorted_array(list_1, list_2):
2+
l1 = 0
3+
l2 = 0
4+
5+
l1_length = len(list_1)
6+
l2_length = len(list_2)
7+
8+
while l1 < l1_length and l2 < l2_length:
9+
if list_1[l1] < list_2[l2]:
10+
l1 += 1
11+
else:
12+
list_1.insert(l1, list_2[l2])
13+
l1 += 1
14+
l2 += 1
15+
16+
if l2 < l2_length:
17+
list_1.extend(list_2[l2:])
18+
19+
return list_1
20+
21+
22+
array_1 = [1, 3, 4, 5]
23+
array_2 = [2, 6, 7, 8]
24+
print(merge_sorted_array(array_1, array_2))
25+
26+
27+

Leetcode/product_except_self.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def product_except_self(arr):
2+
"""
3+
Time Complexity: O(n^2)
4+
Space Complexity: O(1)
5+
"""
6+
result = []
7+
for i, x in enumerate(arr):
8+
product = 1
9+
for j, y in enumerate(arr):
10+
if i != j:
11+
product *= y
12+
result.append(product)
13+
14+
return result
15+
16+
17+
def product_except_self_2(arr):
18+
"""
19+
Time Complexity: O(n)
20+
Space Complexity: O(1)
21+
"""
22+
result = []
23+
size = len(arr)
24+
25+
left = 1
26+
for i in range(0, size):
27+
result.append(left)
28+
left = left * arr[i]
29+
30+
right = 1
31+
for i in range(size-1, -1, -1):
32+
result[i] = result[i] * right
33+
right = right * arr[i]
34+
35+
return result
36+
37+
38+
print(product_except_self([1, 2, 3, 4]))
39+
print(product_except_self_2([1, 2, 3, 4]))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def rearrange(arr):
2+
"""
3+
Time Complexity: O(n)
4+
Space Complexity: O(n)
5+
"""
6+
left = []
7+
right = []
8+
zeros = []
9+
10+
for x in arr:
11+
if x < 0:
12+
left.append(x)
13+
elif x > 0:
14+
right.append(x)
15+
else:
16+
zeros.append(x)
17+
18+
return left + zeros + right
19+
20+
21+
def rearrange_pythonic(arr):
22+
"""
23+
Time Complexity: O(n)
24+
Space Complexity: O(1)
25+
"""
26+
return [i for i in arr if i < 0] + [i for i in arr if i == 0] + [i for i in arr if i > 0]
27+
28+
29+
def rearrange_inplace(arr):
30+
left_most_pos_idx = 0
31+
size = len(arr)
32+
33+
for i in range(size):
34+
if arr[i] < 0:
35+
if i is not left_most_pos_idx:
36+
arr[i], arr[left_most_pos_idx] = arr[left_most_pos_idx], arr[i]
37+
left_most_pos_idx += 1
38+
39+
return arr
40+
41+
42+
print(rearrange([10, -1, 20, 4, 0, 5, -9, -6]))
43+
print(rearrange_pythonic([10, -1, 20, 4, 0, 5, -9, -6]))
44+
print(rearrange_inplace([10, -1, 20, 4, 5, -9, -6]))

0 commit comments

Comments
 (0)