Skip to content

Commit 7a55a4f

Browse files
Updated and cleaned up code
1 parent ec1795f commit 7a55a4f

File tree

6 files changed

+77
-121
lines changed

6 files changed

+77
-121
lines changed

Algorithms/cryptology/ceasar_shifting_cipher/ceasar_shift_cipher.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def decrypt(cipher, shift=3):
4141

4242
return decrypted
4343

44-
def main():
45-
message = 'attackatnoon'
46-
cipher = encrypt(message, shift=3)
47-
decrypted = decrypt(cipher, shift=3)
48-
49-
print('Original message: ' + message)
50-
print('Encrypted message: ' + cipher)
51-
print('Decrypted message: ' + decrypted)
52-
53-
main()
44+
# def main():
45+
# message = 'attackatnoon'
46+
# cipher = encrypt(message, shift=3)
47+
# decrypted = decrypt(cipher, shift=3)
48+
#
49+
# print('Original message: ' + message)
50+
# print('Encrypted message: ' + cipher)
51+
# print('Decrypted message: ' + decrypted)
52+
#
53+
# main()

Algorithms/graphtheory/bellman-ford/bellman_ford.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@
99
2019-03-04 Initial programming
1010
'''
1111

12-
def make_graph(file):
13-
try:
14-
f = open(file, 'r')
15-
except IOError:
16-
raise IOError("File does not exist!")
17-
18-
line_list = f.readlines()
19-
20-
G = {int(line.split()[0]): {(int(tup.split(',')[0])): int(tup.split(',')[1])
21-
for tup in line.split()[1:] if tup} for line in line_list if line}
22-
23-
f.close()
24-
25-
return G
26-
27-
2812
def bellman_ford(G, start):
13+
'''
14+
:param G: {from_node1: {to_node1, cost1, to_node2, cost2}, from_node2: {etc}}
15+
:param start: node to start from
16+
'''
17+
2918
if len(G) == 0:
3019
raise ValueError("There should be something in the graph")
3120

@@ -56,14 +45,12 @@ def bellman_ford(G, start):
5645

5746
return shortest_distance, predecessor
5847

59-
if __name__ == '__main__':
60-
# G = make_graph('data.txt')
61-
62-
G = {1: {2: -10, 3: 20},
63-
2: {4: 40},
64-
3: {4: 5},
65-
4: {}}
66-
67-
print(f'Current graph is: {G}')
68-
shortest, predecessor = bellman_ford(G, 1)
69-
print(shortest)
48+
# if __name__ == '__main__':
49+
# G = {1: {2: -10, 3: 20},
50+
# 2: {4: 40},
51+
# 3: {4: 5},
52+
# 4: {}}
53+
#
54+
# print(f'Current graph is: {G}')
55+
# shortest, predecessor = bellman_ford(G, 1)
56+
# print(shortest)
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from collections import deque
2-
3-
def load_graph(file='exgraph.txt'):
4-
data = open(file, 'r')
5-
G = {}
6-
7-
for line in data:
8-
lst = [int(x) for x in line.split()]
9-
G[lst[0]] = lst[1:]
1+
'''
2+
Programmed by Aladdin Persson
3+
2019-02-17 Initial programming
4+
2020-03-29 Cleaned up code, removed load graph, I think a small example is sufficient
5+
and instead only have the BFS function.
6+
'''
107

11-
num_nodes = len(G)
12-
13-
return G, num_nodes
8+
from collections import deque
149

1510
def BFS(G, start_node=1):
11+
'''
12+
:param G: Graph with G = {from_node1:[to_node1, to_node2], from_node2: [to_node,] etc}
13+
:param start_node: starting node to run BFS from
14+
:return: returns visited boolean array and path in which order it visited them
15+
'''
1616
visited = [False for i in range(1,len(G)+1)]
1717

1818
Q = deque()
@@ -33,17 +33,13 @@ def BFS(G, start_node=1):
3333

3434
return visited, path
3535

36+
# Small Example Run
37+
# if __name__ == '__main__':
38+
# G = {1:[2,3], 2:[1,4], 3:[1,4],4:[]}
39+
# visited, path = BFS(G)
40+
#
41+
# if all(visited) == True:
42+
# print("Return: This graph is connected!")
3643

37-
38-
if __name__ == '__main__':
39-
G, num_nodes = load_graph()
40-
# G = {1:[2,3], 2:[1,4], 3:[1,4],4:[]}
41-
42-
visited, path = BFS(G)
43-
44-
if all(visited) == True:
45-
print("Return: This graph is connected!")
46-
else:
47-
print("Not all nodes were reachable, i.e the graph is not connected.")
48-
49-
44+
# else:
45+
# print("Not all nodes were reachable, i.e the graph is not connected.")

Algorithms/graphtheory/depth-first-search/DFS_recursive.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,31 @@
44
# Programmed by Aladdin Persson
55
# 2019-02-16 Initial programming
66

7-
# Improvements:
8-
# * Check implementation with stack/queue
9-
10-
def load_graph(file='exgraph.txt'):
11-
data = open(file, 'r')
12-
G = {}
13-
14-
for line in data:
15-
lst = [int(x) for x in line.split()]
16-
G[lst[0]] = lst[1:]
17-
18-
num_nodes = len(G)
19-
20-
return G, num_nodes
21-
22-
237
def DFS(G, curr_node, visited):
8+
'''
9+
:param G: G = {from_node1:[to_node1, to_node2], from_node2: [to_node,] etc}
10+
:param curr_node: Node currently at, run from beginning this is the starting node
11+
:param visited: since it is recursive, visited is updated and needs to be sent in on recursive call
12+
:return: visited is initialized outside of DFS and updates this boolean array with which nodes has been visited
13+
'''
2414
if visited[curr_node - 1]:
2515
return
2616

2717
visited[curr_node-1] = True
2818

29-
# G is a dictionary
3019
neighbours = G[curr_node]
3120

3221
for next_node in neighbours:
3322
DFS(G, next_node, visited)
3423

35-
if __name__ == '__main__':
36-
print('Loading graph and print:')
37-
38-
try:
39-
G, num_nodes = load_graph()
40-
print(G)
41-
42-
except TypeError:
43-
raise("Error loading graph.")
44-
45-
# G = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2, 5], 5: [4]}
46-
47-
visited = [False for i in range(1, len(G) + 1)]
48-
start_node = 1
49-
50-
DFS(G, start_node, visited)
51-
52-
if any(visited) == False:
53-
print("Result: This graph is connected!")
24+
# Small Eaxmple
25+
# if __name__ == '__main__':
26+
# G = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2, 5], 5: [4]}
27+
#
28+
# visited = [False for i in range(1, len(G) + 1)]
29+
# start_node = 1
30+
#
31+
# DFS(G, start_node, visited)
32+
#
33+
# if any(visited) == False:
34+
# print("Result: This graph is connected!")

Algorithms/graphtheory/depth-first-search/DFS_stack_iterative.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
2020-03-29 Cleaned up code, made test cases
88
'''
99

10-
11-
def load_graph(file='exgraph.txt'):
12-
data = open(file, 'r')
13-
G = {}
14-
15-
for line in data:
16-
lst = [int(x) for x in line.split()]
17-
G[lst[0]] = lst[1:]
18-
19-
num_nodes = len(G)
20-
21-
return G, num_nodes
22-
2310
def DFS(G, start_node):
11+
'''
12+
:param G: Graph with G = {from_node1:[to_node1, to_node2], from_node2: [to_node,] etc}
13+
:param start_node: starting node to run BFS from
14+
:return: returns visited boolean array and path in which order it visited them
15+
'''
2416
visited = [False for i in range(1, len(G) + 1)]
2517
path = [start_node]
2618
stack = []
@@ -38,12 +30,12 @@ def DFS(G, start_node):
3830

3931
return visited, path
4032

41-
if __name__ == '__main__':
42-
G, num_nodes = load_graph()
43-
start_node = 1
44-
visited = DFS(G, start_node)
45-
46-
if all(visited) == True:
47-
print("Return: This graph is connected!")
48-
else:
49-
print("Not all nodes were reachable, i.e the graph is not connected.")
33+
# if __name__ == '__main__':
34+
# G = {1: [2, 3], 2: [1, 4], 3: [1, 4], 4: []}
35+
# start_node = 1
36+
# visited = DFS(G, start_node)
37+
#
38+
# if all(visited) == True:
39+
# print("Return: This graph is connected!")
40+
# else:
41+
# print("Not all nodes were reachable, i.e the graph is not connected.")

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Whenever I face an interesting problem I document the algorithm that I learned t
5959

6060
# Sorting algorithms
6161
* :white_check_mark: [Bubble sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/bubblesort.py) **- O(n<sup>2sup>)**
62-
* :small_red_triangle: [Hope sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/hopesort.py) **- O(1), hopefully **
62+
* :small_red_triangle: [Hope sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/hopesort.py) **- O(1), hopefully**
6363
* :white_check_mark: [Insertion sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/insertionsort.py) **- O(n<sup>2sup>)**
6464
* :white_check_mark: [Selection sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/selectionsort.py) **- O(n<sup>2sup>)**
6565
* :white_check_mark: [Merge sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/sorting/mergesort.py) **- O(nlog(n))**

0 commit comments

Comments
 (0)