Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bellman-ford python implementation and BFS #454

Merged
merged 3 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
101 changes: 101 additions & 0 deletions Graphs/Bellman-ford/python/bellman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class OrientedGraph:
def __init__(self):
self.nodes = {}

def insertNode(self, u):
if u not in self.nodes:
self.nodes[u] = {}

def V(self):
return self.nodes.keys()

def adj(self, u):
if u in self.nodes:
return self.nodes[u]

def insertEdge(self, u, v, w=0):
if u not in self.nodes:
self.insertNode(u)
if v not in self.nodes:
self.insertNode(v)
self.nodes[u][v] = w

def bfs(self, start):
S = [start]
visited = {}
for node in self.V():
visited[node] = False
visited[start] = True
while len(S) > 0:
u = S.pop(0)
for v in self.adj(u):
print u, v, self.nodes[u][v]
if not visited[v]:
visited[v] = True
S.append(v)

def dfs(self, start):
S = [start]
visited = {}
for node in self.V():
visited[node] = False
visited[start] = True
while len(S) > 0:
u = S.pop(-1)
for v in self.adj(u):
print u, v
if not visited[v]:
visited[v] = True
S.append(v)

def __str__(self):
string = ""
for u in self.V():
string += (str(u) + "->" + str(self.adj(u)) + "\n")
return string

def init_bellman(self, source):
d = {} # Stands for destination
p = {} # Stands for predecessor
for node in self.nodes:
d[node] = float('Inf') # We start admiting that the rest of nodes are very very far
p[node] = None
d[source] = 0 # For the source we know how to reach
return d, p

def relax(self, node, neighbour, graph, d, p):
if d[neighbour] > d[node] + graph[node][neighbour]:
# Record this lower distance
d[neighbour] = d[node] + graph[node][neighbour]
p[neighbour] = node

def bellman_ford(self, start):
graph = self.nodes
d, p = self.init_bellman(start)
for i in range(len(graph) - 1): #Run this until is converges
for u in graph:
for v in graph[u]:
self.relax(u, v, graph, d, p) # Lets relax it

for u in graph:
for v in graph[u]:
assert d[v] <= d[u] + graph[u][v]

print "Costs to reach destination from", start, "after bellman-ford: ", d
return d, p


def init():
graph = OrientedGraph()
for u in ['a', 'b', 'c', 'd', 'e', 'f']:
graph.insertNode(u)
for u, v, w in [('a', 'b', 3), ('a', 'd', 2), ('b', 'c', 0), ('d', 'a', 1), ('d', 'c', 6), ('d', 'e', 10), ('e', 'c', 1), ('e', 'f', 0)]:
graph.insertEdge(u, v, w)
print str(graph)
# Test bellman starting from a
graph.bellman_ford('a')
return


if __name__ == "__main__":
init()
112 changes: 112 additions & 0 deletions Graphs/bfs/python/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
class Graph:
def __init__(self):
self.nodes = {}

def insertNode(self, u):
if u not in self.nodes:
self.nodes[u] = {}

def V(self):
return self.nodes.keys()

def adj(self, u):
if u in self.nodes:
return self.nodes[u]

def insertEdge(self, u, v, w=0):
if u not in self.nodes:
self.insertNode(u)
if v not in self.nodes:
self.insertNode(v)
self.nodes[u][v] = w
self.nodes[v][u] = w

def bfs(self, start):
S = [start]
visited = {}
for node in self.V():
visited[node] = False
visited[start] = True
while len(S) > 0:
u = S.pop(0)
for v in self.adj(u):
print "Start \t", u, " | Destination:\t", v
if not visited[v]:
visited[v] = True
S.append(v)

def __str__(self):
string = ""
for u in self.V():
string += (str(u) + "->" + str(self.adj(u)) + "\n")
return string


class OrientedGraph:
def __init__(self):
self.nodes = {}

def insertNode(self, u):
if u not in self.nodes:
self.nodes[u] = {}

def V(self):
return self.nodes.keys()

def adj(self, u):
if u in self.nodes:
return self.nodes[u]

def insertEdge(self, u, v, w=0):
if u not in self.nodes:
self.insertNode(u)
if v not in self.nodes:
self.insertNode(v)
self.nodes[u][v] = w

def bfs(self, start):
S = [start]
visited = {}
for node in self.V():
visited[node] = False
visited[start] = True
while len(S) > 0:
u = S.pop(0)
for v in self.adj(u):
print "Start \t", u, " | Destination:\t", v, " | Cost:\t", self.nodes[u][v]
if not visited[v]:
visited[v] = True
S.append(v)

def __str__(self):
string = ""
for u in self.V():
string += (str(u) + "->" + str(self.adj(u)) + "\n")
return string


def init():
print "Test for non-oriented graph"
graph = Graph()
for u in ['a', 'b', 'c', 'd', 'e', 'f']:
graph.insertNode(u)
for u, v in [('a', 'b'), ('a', 'd'), ('b', 'c'), ('d', 'a'), ('d', 'c'), ('d', 'e'), ('e', 'c'), ('e', 'f')]:
graph.insertEdge(u, v)
print str(graph)
# Test bfs starting from a
graph.bfs('a')
print "---------------------------------------------"
print "\nTest for Oriented Graph"
graph = Graph()
for u in ['a', 'b', 'c', 'd', 'e', 'f']:
graph.insertNode(u)
for u, v, w in [('a', 'b', 3), ('a', 'd', 2), ('b', 'c', 0), ('d', 'a', 1), ('d', 'c', 6), ('d', 'e', 10), ('e', 'c', 1), ('e', 'f', 0)]:
graph.insertEdge(u, v, w)
print str(graph)
# Test bellman starting from a
graph.bfs('a')
return


if __name__ == '__main__':
init()