Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72e4dce

Browse files
committed
Completed Exercises
1 parent cd70b41 commit 72e4dce

File tree

10 files changed

+137
-10
lines changed

10 files changed

+137
-10
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
def count_edges(matrix):
2-
return
2+
edge_count = 0
3+
for i in range(len(matrix)):
4+
for j in range(len(matrix[i])):
5+
edge_count += matrix[i][j]
6+
return edge_count
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
def count_islands(adjacency_list):
2-
return
2+
dictionary_count_edges = dict.fromkeys(adjacency_list.keys(), 0)
3+
for adj_keyNode, edge_list in adjacency_list.items():
4+
for keyNode in dictionary_count_edges.keys():
5+
if not adj_keyNode == keyNode:
6+
if keyNode in edge_list:
7+
dictionary_count_edges[keyNode] += 1
8+
number_of_Islands = 0
9+
for edgeCount in dictionary_count_edges.values():
10+
if edgeCount == 0:
11+
number_of_Islands += 1
12+
return number_of_Islands
13+
14+
#adjacency_list = { 0: [1], 1: [0, 1, 2], 2: []}
15+
16+
# dictionary_count_edges -> assigns every key:value pair to 0
17+
18+
#dictionary_count_edges = {0:0, 1:0, 2: 0}
19+
#for every key and list in adjacency_list
20+
#0 : [1]
21+
#1 : [0, 1, 2]
22+
#2 : []
23+
24+
#dictionary_count_edges = {0:2, 1:1, 2:1}
25+
# for every key in dictionary_count_edges
26+
# 0 -> in 0 and 1 so increment by 2
27+
# 1 -> in 0 so increment by 1
28+
# 2 -> in 1 so increment by 1
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
adjacency_matrix = []
1+
adjacency_matrix = [
2+
[1, 1, 0, 1, 0],
3+
[0, 0, 0, 1, 1],
4+
[0, 1, 0, 0, 0],
5+
[1, 0, 1, 0, 0],
6+
[0, 0, 0, 0, 0],
7+
]
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
adjacency_list = {}
1+
adjacency_list = {
2+
0: [1,2,3],
3+
1: [0,3],
4+
2: [1],
5+
3: [0,3]
6+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
adjacency_list = {}
1+
adjacency_list = {
2+
0: [],
3+
1: [2],
4+
2: [1, 3],
5+
3: [1]
6+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
adjacency_matrix = []
1+
adjacency_matrix = [
2+
[0, 1, 0],
3+
[1, 0, 1],
4+
[0, 1, 0]
5+
]
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
def most_neighbours(adjacency_list):
2-
return
2+
maximum_len = -1
3+
maximum_key = -1
4+
for keyNode, edge_list in adjacency_list.items():
5+
number_of_neighbours = len(edge_list)
6+
if number_of_neighbours > maximum_len:
7+
maximum_key = keyNode
8+
maximum_len = number_of_neighbours
9+
elif number_of_neighbours == maximum_len and keyNode < maximum_key:
10+
maximum_key = keyNode
11+
return maximum_key
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
def hops_away(adjacency_list, node, num_hops):
2-
return
2+
if not node in adjacency_list.keys():
3+
return []
4+
elif num_hops == 0:
5+
return [node]
6+
else:
7+
number_hops_away = []
8+
for neighbour in adjacency_list[node]:
9+
number_hops_away.extend(hops_away(adjacency_list, neighbour, num_hops-1))
10+
number_hops_away_1 = []
11+
for n in number_hops_away:
12+
if not n in number_hops_away_1:
13+
number_hops_away_1.append(n)
14+
return number_hops_away_1
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
def exists_path(matrix, origin, destination):
2-
return
2+
def exists_path_visited(matrix, origin, destination, visited):
3+
if origin in visited:
4+
return False
5+
if not (origin in range(len(matrix)) and
6+
destination in range(len(matrix))):
7+
return False
8+
elif matrix[origin][destination] == 1:
9+
return True
10+
visited.append(origin)
11+
for j in range(len(matrix[origin])):
12+
if matrix[origin][j] == 1 and exists_path_visited(matrix, j, destination, visited):
13+
return True
14+
return False
15+
return exists_path_visited(matrix, origin, destination, [])
16+
17+
# matrix = [ [0, 1, 0], [0, 0, 1], [0, 1, 0] ]
18+
19+
# exists_path(matrix, 0, 2) => True
20+
21+
# exists_path_visited(matrix, 0, 2, [])
22+
23+
# visited = [0]
24+
# for j = 0, 1, 2
25+
# matrix, j, destination, visitedlist
26+
# exists_path_visited(matrix, 1, 2, [0])
27+
28+
#matrix[1][2] == 1, so we would return True
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1+
def reachable_dict(matrix):
2+
reachable_dict = {}
3+
for i in range(len(matrix)):
4+
reachable_lst = []
5+
for j in range(len(matrix[i])):
6+
if matrix[i][j] == 1:
7+
reachable_lst.append(j)
8+
reachable_dict[i] = reachable_lst
9+
#getting every key that's directly reachable and putting it into a list
10+
all_reachable = {}
11+
for i, neighbours in reachable_dict.items():
12+
all_reachable_lst = []
13+
for n in neighbours:
14+
all_reachable_lst = all_reachable_lst + reachable_dict[n]
15+
all_reachable_lst = all_reachable_lst + [n]
16+
all_reachable[i] = all_reachable_lst
17+
return all_reachable
18+
#getting every key that's neighbors of the directly reachables and putting it into a list
19+
120
def num_components(adjacency_matrix):
2-
return
21+
reachableDict = reachable_dict(adjacency_matrix)
22+
num_components = 0
23+
visited = [False for i in range(len(adjacency_matrix))]
24+
for i in range(len(adjacency_matrix)):
25+
if visited[i] == False:
26+
num_components += 1
27+
for j in reachableDict[i]:
28+
visited[j] = True
29+
return num_components
30+
31+
# visited [False, False, False, False]
32+
# visited []

0 commit comments

Comments
 (0)