Skip to content

Commit 818c400

Browse files
committed
Add files vai upload
1 parent 3f96fa9 commit 818c400

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2666
-0
lines changed

Chapter01/chapter1.py

Lines changed: 578 additions & 0 deletions
Large diffs are not rendered by default.

Chapter02/binary_search.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def binary_search(arr, low, high, key):
2+
while low <= high:
3+
mid = int(low + (high - low)/2)
4+
if arr[mid] == key:
5+
return mid
6+
elif arr[mid] < key:
7+
low = mid + 1
8+
else:
9+
high = mid - 1
10+
return -1
11+
12+
13+
arr = [ 2, 3, 4, 2, 10, 40]
14+
x = 10
15+
result = binary_search(arr, 0, len(arr)-1, x)
16+
print(result)

Chapter02/linear_search.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Linear search program to search an element, return the index position of the #array
2+
3+
def searching(search_arr, x):
4+
for i in range(len(search_arr)):
5+
if search_arr[i] == x:
6+
return i
7+
return -1
8+
9+
10+
search_arr = [3, 4, 1, 6, 14]
11+
x=4
12+
print("Index position for the element x is:", searching(search_arr, x))

Chapter02/matrix_chain.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
3+
def matrix_chain(mat, i, j):
4+
if i == j:
5+
return 0
6+
minimum_computations = sys.maxsize
7+
for k in range(i, j):
8+
count = (MatrixChain(mat, i, k) + MatrixChain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j])
9+
if count < minimum_computations:
10+
minimum_computations = count
11+
return minimum_computations
12+
13+
matrix_sizes = [20, 30, 45, 50]
14+
print("Minimum multiplications are", MatrixChain(matrix_sizes , 1, len(matrix_sizes)-1))

Chapter02/merge_sort.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def merge_sort(unsorted_list):
2+
if len(unsorted_list) == 1:
3+
return unsorted_list
4+
mid_point = int(len(unsorted_list)/2)
5+
first_half = unsorted_list[:mid_point]
6+
second_half = unsorted_list[mid_point:]
7+
8+
half_a = merge_sort(first_half)
9+
half_b = merge_sort(second_half)
10+
11+
return merge(half_a, half_b)
12+
13+
14+
def merge(first_sublist, second_sublist):
15+
i = j = 0
16+
merged_list = []
17+
while i < len(first_sublist) and j < len(second_sublist):
18+
if first_sublist[i] < second_sublist[j]:
19+
merged_list.append(first_sublist[i])
20+
i += 1
21+
else:
22+
merged_list.append(second_sublist[j])
23+
j += 1
24+
25+
while i < len(first_sublist):
26+
merged_list.append(first_sublist[i])
27+
i += 1
28+
29+
while j < len(second_sublist):
30+
merged_list.append(second_sublist[j])
31+
j += 1
32+
return merged_list
33+
34+
35+
a= [11, 12, 7, 41, 61, 13, 16, 14]
36+
print(merge_sort(a))
37+

Chapter03/Fibonacci_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def fib(n):
2+
if n <= 1:
3+
return 1
4+
else:
5+
return fib(n-1) + fib(n-2)
6+
7+
for i in range(5):
8+
print(fib(i))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
def MatrixChain(mat, i, j):
3+
if i == j:
4+
return 0
5+
minimum_computations = sys.maxsize
6+
for k in range(i, j):
7+
count = (MatrixChain(mat, i, k) + MatrixChain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j])
8+
if count < minimum_computations:
9+
minimum_computations = count
10+
return minimum_computations
11+
12+
13+
matrix_sizes = [20, 30, 45, 50]
14+
print("Minimum multiplications are", MatrixChain(matrix_sizes , 1, len(matrix_sizes)-1))

Chapter03/binary_search.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def binary_search(arr, low, high, key):
2+
while low <= high:
3+
mid = int(low + (high - low)/2)
4+
if arr[mid] == key:
5+
return mid
6+
elif arr[mid] < key:
7+
low = mid + 1
8+
else:
9+
high = mid - 1
10+
return -1
11+
12+
13+
arr = [ 2, 3, 4, 2, 10, 40]
14+
x = 10
15+
result = binary_search(arr, 0, len(arr)-1, x)
16+
print(result)

Chapter03/dijkstra.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
def get_shortest_distance(table, vertex):
3+
shortest_distance = table[vertex][DISTANCE]
4+
return shortest_distance
5+
6+
def set_shortest_distance(table, vertex, new_distance):
7+
table[vertex][DISTANCE] = new_distance
8+
9+
def set_previous_node(table, vertex, previous_node):
10+
table[vertex][PREVIOUS_NODE] = previous_node
11+
12+
def get_distance(graph, first_vertex, second_vertex):
13+
return graph[first_vertex][second_vertex]
14+
15+
def get_next_node(table, visited_nodes):
16+
unvisited_nodes = list(set(table.keys()).difference(set(visited_nodes)))
17+
assumed_min = table[unvisited_nodes[0]][DISTANCE]
18+
min_vertex = unvisited_nodes[0]
19+
for node in unvisited_nodes:
20+
if table[node][DISTANCE] < assumed_min:
21+
assumed_min = table[node][DISTANCE]
22+
min_vertex = node
23+
return min_vertex
24+
25+
def find_shortest_path(graph, table, origin):
26+
visited_nodes = []
27+
current_node = origin
28+
starting_node = origin
29+
while True:
30+
adjacent_nodes = graph[current_node]
31+
if set(adjacent_nodes).issubset(set(visited_nodes)):
32+
# Nothing here to do. All adjacent nodes have been visited.
33+
pass
34+
else:
35+
unvisited_nodes = set(adjacent_nodes).difference(set(visited_nodes))
36+
for vertex in unvisited_nodes:
37+
distance_from_starting_node = get_shortest_distance(table, vertex)
38+
if distance_from_starting_node == INFINITY and current_node == starting_node:
39+
total_distance = get_distance(graph, vertex, current_node)
40+
else:
41+
total_distance = get_shortest_distance (table,
42+
current_node) + get_distance(graph, current_node, vertex)
43+
if total_distance < distance_from_starting_node:
44+
set_shortest_distance(table, vertex, total_distance)
45+
set_previous_node(table, vertex, current_node)
46+
visited_nodes.append(current_node)
47+
#print(visited_nodes)
48+
if len(visited_nodes) == len(table.keys()):
49+
break
50+
current_node = get_next_node(table,visited_nodes)
51+
return(table)
52+
53+
graph = dict()
54+
graph['A'] = {'B': 5, 'D': 9, 'E': 2}
55+
graph['B'] = {'A': 5, 'C': 2}
56+
graph['C'] = {'B': 2, 'D': 3}
57+
graph['D'] = {'A': 9, 'F': 2, 'C': 3}
58+
graph['E'] = {'A': 2, 'F': 3}
59+
graph['F'] = {'E': 3, 'D': 2}
60+
61+
table = dict()
62+
table = {
63+
'A': [0, None],
64+
'B': [float("inf"), None],
65+
'C': [float("inf"), None],
66+
'D': [float("inf"), None],
67+
'E': [float("inf"), None],
68+
'F': [float("inf"), None],
69+
}
70+
71+
72+
DISTANCE = 0
73+
PREVIOUS_NODE = 1
74+
INFINITY = float('inf')
75+
76+
shortest_distance_table = find_shortest_path(graph, table, 'A')
77+
78+
79+
for k in sorted(shortest_distance_table):
80+
print("{} - {}".format(k,shortest_distance_table[k]))

Chapter03/dyna_fib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def dyna_fib(n, lookup):
2+
if n == 0:
3+
return 0
4+
if n <= 2:
5+
lookup[n] = 1
6+
if lookup[n] is None:
7+
lookup[n] = dyna_fib(n-1, lookup) + dyna_fib(n-2, lookup)
8+
return lookup[n]
9+
10+
lookup = [None]*(1000)
11+
12+
for i in range(6):
13+
print(dyna_fib(i, lookup))

0 commit comments

Comments
 (0)