forked from bloominstituteoftechnology/Graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from shaunorpen/shaun-orpen
WEBEU3 - Graphs - Shaun Orpen
- Loading branch information
Showing
5 changed files
with
247 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
|
||
def earliest_ancestor(ancestors, starting_node): | ||
pass | ||
def get_parent(ancestors, starting_node): | ||
for (parent, child) in ancestors: | ||
if child == starting_node: | ||
return parent | ||
|
||
def earliest_ancestor(ancestors, starting_node, iterations = 0): | ||
parent = get_parent(ancestors, starting_node) | ||
if parent is None: | ||
if iterations == 0: | ||
return -1 | ||
else: | ||
return starting_node | ||
else: | ||
iterations += 1 | ||
return earliest_ancestor(ancestors, parent, iterations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# Note: This Queue class is sub-optimal. Why? | ||
class Queue(): | ||
def __init__(self): | ||
self.queue = [] | ||
def enqueue(self, value): | ||
self.queue.append(value) | ||
def dequeue(self): | ||
if self.size() > 0: | ||
return self.queue.pop(0) | ||
else: | ||
return None | ||
def size(self): | ||
return len(self.queue) | ||
|
||
class Stack(): | ||
def __init__(self): | ||
self.stack = [] | ||
def push(self, value): | ||
self.stack.append(value) | ||
def pop(self): | ||
if self.size() > 0: | ||
return self.stack.pop() | ||
else: | ||
return None | ||
def size(self): | ||
return len(self.stack) | ||
|