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
Changes from 1 commit
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
Prev Previous commit
graph added
  • Loading branch information
ItsAdityaKSingh committed Oct 4, 2021
commit 00170d4508c63ec647e1620be23e5dea7656f2ec
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()])
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy