Skip to content

Commit 6fc3ce3

Browse files
committed
Finished graph chapter, on case studies of algo chapter
1 parent 1dbcffb commit 6fc3ce3

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

6-graphs/EulerianHamiltonian.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Eulerian and Hamiltonian Paths and Cycles
2+
3+
The lecturer sort of switched back and forth between saying paths and cycles and didn't emphasize vertices vs edges hard enough, which was kind of annoying and confusingly inconsistent.
4+
5+
Basically, a Eulerian (pronounced Oilerian) path visits every EDGE exactly one time, starting and ending anywhere. A Eulerian cycle visits every EDGE exactly one time, starting and ending at the same node.
6+
7+
A Hamiltonian path visits every NODE (VERTEX) exactly one time, starting and ending anywhere. A Hamiltonian cycle visits every NODE exactly one time, starting and ending at the same node.
8+
9+
### Algo for Eulerian
10+
11+
Possible algo for finding Eulerian paths is:
12+
13+
1. Start at any vertex and follow edges until you return back to that vertex.
14+
2. If you haven't yet seen every edge, then do an unseen edge connected to the node you already visited. Do the same thing.
15+
3. Once you have seen every edge, combine the paths at the nodes that they have in common.
16+
17+
O(# of edges)

6-graphs/GraphTraversalQuizAnswers.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Graph(object):
2+
3+
def dfs_helper(self, start_node):
4+
"""The helper function for a recursive implementation
5+
of Depth First Search iterating through a node's edges. The
6+
output should be a list of numbers corresponding to the
7+
values of the traversed nodes.
8+
ARGUMENTS: start_node is the starting Node
9+
REQUIRES: self._clear_visited() to be called before
10+
MODIFIES: the value of the visited property of nodes in self.nodes
11+
RETURN: a list of the traversed node values (integers).
12+
"""
13+
ret_list = [start_node.value]
14+
start_node.visited = True
15+
edges_out = [e for e in start_node.edges
16+
if e.node_to.value != start_node.value]
17+
for edge in edges_out:
18+
if not edge.node_to.visited:
19+
ret_list.extend(self.dfs_helper(edge.node_to))
20+
return ret_list
21+
22+
def bfs(self, start_node_num):
23+
"""An iterative implementation of Breadth First Search
24+
iterating through a node's edges. The output should be a list of
25+
numbers corresponding to the traversed nodes.
26+
ARGUMENTS: start_node_num is the node number (integer)
27+
MODIFIES: the value of the visited property of nodes in self.nodes
28+
RETURN: a list of the node values (integers)."""
29+
node = self.find_node(start_node_num)
30+
self._clear_visited()
31+
ret_list = []
32+
# Your code here
33+
queue = [node]
34+
node.visited = True
35+
def enqueue(n, q=queue):
36+
n.visited = True
37+
q.append(n)
38+
def unvisited_outgoing_edge(n, e):
39+
return ((e.node_from.value == n.value) and
40+
(not e.node_to.visited))
41+
while queue:
42+
node = queue.pop(0)
43+
ret_list.append(node.value)
44+
for e in node.edges:
45+
if unvisited_outgoing_edge(node, e):
46+
enqueue(e.node_to)
47+
return ret_list

7-case-studies/Dijkstra.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
### Shortest Path Problem
2+
3+
The shortest path in an unweighted graph is just the path with the fewest number of edges. This is a breadth first search, and whichever one you find first is the shortest one.
4+
5+
### Dijkstra's Algorithm
6+
7+
Example is for a weighted graph.
8+
9+
Greedy algorithm that uses a min priority queue to extract the minimum, queue up paths to other nodes with info on how long they take. Node info gets updated with the lowest path if another lower one is found.
10+
11+
Adjacency matrix representation of this weighted graph example:
12+
13+
U D A C I T Y
14+
U 0 3 4 6 0 0 0
15+
D 3 0 0 4 0 0 0
16+
A 4 0 0 0 7 0 0
17+
C 6 4 0 0 4 0 0
18+
I 0 4 7 4 0 0 4
19+
T 0 0 0 0 0 0 5
20+
Y 0 0 0 0 4 5 0
21+
22+
Dijkstra's algo calculation by step
23+
24+
U D A C I T Y
25+
0 inf -------->
26+
x 3 4 6 inf---->
27+
x x 4 6 inf ---->
28+
x x x 6 11 inf -->
29+
x x x x 10 11 inf
30+
x x x x x 11 14
31+
x x x x x x 14
32+
33+

0 commit comments

Comments
 (0)