Skip to content

Added queue and graphs to data structures #135

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
83 changes: 83 additions & 0 deletions algorithms/data-structures/graphs/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Vertex:
def __init__(self, node):
self.id = node
self.adjacent = {}

def __str__(self):
return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])

def add_neighbor(self, neighbor, weight=0):
self.adjacent[neighbor] = weight

def get_connections(self):
return self.adjacent.keys()

def get_id(self):
return self.id

def get_weight(self, neighbor):
return self.adjacent[neighbor]

class Graph:
def __init__(self):
self.vert_dict = {}
self.num_vertices = 0

def __iter__(self):
return iter(self.vert_dict.values())

def add_vertex(self, node):
self.num_vertices = self.num_vertices + 1
new_vertex = Vertex(node)
self.vert_dict[node] = new_vertex
return new_vertex

def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None

def add_edge(self, frm, to, cost = 0):
if frm not in self.vert_dict:
self.add_vertex(frm)
if to not in self.vert_dict:
self.add_vertex(to)

self.vert_dict[frm].add_neighbor(self.vert_dict[to], cost)
self.vert_dict[to].add_neighbor(self.vert_dict[frm], cost)

def get_vertices(self):
return self.vert_dict.keys()


# Working of graph is as mentioned:
# if __name__ == '__main__':

# g = Graph()

# g.add_vertex('a')
# g.add_vertex('b')
# g.add_vertex('c')
# g.add_vertex('d')
# g.add_vertex('e')
# g.add_vertex('f')

# g.add_edge('a', 'b', 7)
# g.add_edge('a', 'c', 9)
# g.add_edge('a', 'f', 14)
# g.add_edge('b', 'c', 10)
# g.add_edge('b', 'd', 15)
# g.add_edge('c', 'd', 11)
# g.add_edge('c', 'f', 2)
# g.add_edge('d', 'e', 6)
# g.add_edge('e', 'f', 9)

# for v in g:
# for w in v.get_connections():
# vid = v.get_id()
# wid = w.get_id()
# print '( %s , %s, %3d)' % ( vid, wid, v.get_weight(w))

# for v in g:
# print 'g.vert_dict[%s]=%s' %(v.get_id(), g.vert_dict[v.get_id()])
39 changes: 39 additions & 0 deletions algorithms/data-structures/queues/queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Queue implementation in Python

class Queue:

def __init__(self):
self.queue = []

# Add an element
def enqueue(self, item):
self.queue.append(item)

# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)

# Display the queue
def display(self):
print(self.queue)

def size(self):
return len(self.queue)

# For general working of a queue in python:
# q = Queue()
# q.enqueue(1)
# q.enqueue(2)
# q.enqueue(3)
# q.enqueue(4)
# q.enqueue(5)

# q.display()

# q.dequeue()

# print("After removing an element")
# q.display()