Skip to content

Commit 48ce03b

Browse files
committed
Graph notes and quiz for first half
1 parent f04c5fa commit 48ce03b

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

6-graphs/GraphNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ Connectivity, in Graph Theory, indicates the level of connections between nodes.
2525
A directed graph is weakly connected when only replacing all of the directed edges with undirected edges can cause it to be connected. So if you have a vertex that has an outbound edge but not an inbound edge, and if you replace that edge with a undirected edge it allows connectivity to that vertex, then that graph was weakly connected.
2626

2727
Strongly connected directed graphs have a path from every node to every other node. A to B and B to A.
28+
29+
### Quiz
30+
31+
Cycles in the provided graph were 3, and it was weakly connected. Odd because I can count 4, so maybe I'm not sure what counts as a cycle?

6-graphs/GraphQuiz-mine.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Node(object):
2+
def __init__(self, value):
3+
self.value = value
4+
self.edges = []
5+
6+
class Edge(object):
7+
def __init__(self, value, node_from, node_to):
8+
self.value = value
9+
self.node_from = node_from
10+
self.node_to = node_to
11+
12+
class Graph(object):
13+
def __init__(self, nodes=[], edges=[]):
14+
self.nodes = nodes
15+
self.edges = edges
16+
17+
def insert_node(self, new_node_val):
18+
new_node = Node(new_node_val)
19+
self.nodes.append(new_node)
20+
21+
def insert_edge(self, new_edge_val, node_from_val, node_to_val):
22+
from_found = None
23+
to_found = None
24+
for node in self.nodes:
25+
if node_from_val == node.value:
26+
from_found = node
27+
if node_to_val == node.value:
28+
to_found = node
29+
if from_found == None:
30+
from_found = Node(node_from_val)
31+
self.nodes.append(from_found)
32+
if to_found == None:
33+
to_found = Node(node_to_val)
34+
self.nodes.append(to_found)
35+
new_edge = Edge(new_edge_val, from_found, to_found)
36+
from_found.edges.append(new_edge)
37+
to_found.edges.append(new_edge)
38+
self.edges.append(new_edge)
39+
40+
def get_edge_list(self):
41+
"""Don't return a list of edge objects!
42+
Return a list of triples that looks like this:
43+
(Edge Value, From Node Value, To Node Value)"""
44+
edge_list = []
45+
for edge in self.edges:
46+
edge_list.append((edge.value, edge.node_from.value, edge.node_to.value))
47+
return edge_list
48+
49+
def get_adjacency_list(self):
50+
"""Don't return any Node or Edge objects!
51+
You'll return a list of lists.
52+
The indecies of the outer list represent
53+
"from" nodes.
54+
Each section in the list will store a list
55+
of tuples that looks like this:
56+
(To Node, Edge Value)"""
57+
adjacency_list = [None]*(len(self.nodes)+1)
58+
for edge in self.edges:
59+
if not adjacency_list[edge.node_from.value]:
60+
adjacency_list[edge.node_from.value] = [(edge.node_to.value, edge.value)]
61+
else:
62+
adjacency_list[edge.node_from.value].append((edge.node_to.value, edge.value))
63+
return adjacency_list
64+
65+
def get_adjacency_matrix(self):
66+
"""Return a matrix, or 2D list.
67+
Row numbers represent from nodes,
68+
column numbers represent to nodes.
69+
Store the edge values in each spot,
70+
and a 0 if no edge exists."""
71+
adjacency_matrix = [[0 for x in range(len(self.nodes)+1)] for y in range(len(self.nodes)+1)]
72+
for edge in self.edges:
73+
adjacency_matrix[edge.node_from.value][edge.node_to.value] = edge.value
74+
return adjacency_matrix
75+
76+
graph = Graph()
77+
graph.insert_edge(100, 1, 2)
78+
graph.insert_edge(101, 1, 3)
79+
graph.insert_edge(102, 1, 4)
80+
graph.insert_edge(103, 3, 4)
81+
# Should be [(100, 1, 2), (101, 1, 3), (102, 1, 4), (103, 3, 4)]
82+
print graph.get_edge_list()
83+
# Should be [None, [(2, 100), (3, 101), (4, 102)], None, [(4, 103)], None]
84+
print graph.get_adjacency_list()
85+
# Should be [[0, 0, 0, 0, 0], [0, 0, 100, 101, 102], [0, 0, 0, 0, 0], [0, 0, 0, 0, 103], [0, 0, 0, 0, 0]]
86+
print graph.get_adjacency_matrix()

6-graphs/GraphQuiz-solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def get_edge_list(self):
2+
edge_list = []
3+
for edge_object in self.edges:
4+
edge = (edge_object.value, edge_object.node_from.value, edge_object.node_to.value)
5+
edge_list.append(edge)
6+
return edge_list
7+
8+
def get_adjacency_list(self):
9+
max_index = self.find_max_index()
10+
adjacency_list = [None] * (max_index + 1)
11+
for edge_object in self.edges:
12+
if adjacency_list[edge_object.node_from.value]:
13+
adjacency_list[edge_object.node_from.value].append((edge_object.node_to.value, edge_object.value))
14+
else:
15+
adjacency_list[edge_object.node_from.value] = [(edge_object.node_to.value, edge_object.value)]
16+
return adjacency_list
17+
18+
def get_adjacency_matrix(self):
19+
max_index = self.find_max_index()
20+
adjacency_matrix = [[0 for i in range(max_index + 1)] for j in range(max_index + 1)]
21+
for edge_object in self.edges:
22+
adjacency_matrix[edge_object.node_from.value][edge_object.node_to.value] = edge_object.value
23+
return adjacency_matrix
24+
25+
def find_max_index(self):
26+
max_index = -1
27+
if len(self.nodes):
28+
for node in self.nodes:
29+
if node.value > max_index:
30+
max_index = node.value
31+
return max_index

6-graphs/GraphRepNotes.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Edge List
2+
3+
A 2D list that shows a list of edges. It lists the vertices/nodes that are connected to each other.
4+
[[0,1],[1,2],[1,3],[2,3]]
5+
6+
### Adjacency List
7+
8+
Each node corresponds to an index in an array.
9+
10+
0 1 2 3
11+
[[1],[0,2,3],[1,3],[1,2]]
12+
13+
### Adjacency Matrix
14+
15+
for each node, 1 if connected, 0 if not neighbors.
16+
0 1 2 3
17+
0 [[0,1,0,0],
18+
1 [1,0,1,1],
19+
2 [0,1,0,1],
20+
3 [0,1,1,0]]

0 commit comments

Comments
 (0)