


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Depth First Search, Deepest Vertex, Traversing, Graph, Starting Vertex, Dead End, Finding, Distance, Type of Search, Implementation
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



""" File: graph_algorithms.py """
from graph import Graph from vertex import Vertex from linked_queue import LinkedQueue
def bfs(g,start): start.setDistance(0) start.setPred(None) vertQueue = LinkedQueue() vertQueue.enqueue(start) while (vertQueue.size() > 0): currentVert = vertQueue.dequeue() for nbr in currentVert.getConnections(): if (nbr.getColor() == 'white'): nbr.setColor('gray') nbr.setDistance(currentVert.getDistance()+1) nbr.setPred(currentVert) vertQueue.enqueue(nbr) currentVert.setColor('black')
""" File: vertex.py """ class Vertex: def init(self, key, color = 'white', dist = 0, pred = None): self.id = key self.connectedTo = {} self.color = color self.predecessor = pred self.distance = dist self.discovery = 0 self.finish = 0
def addNeighbor(self,nbr,weight=0): self.connectedTo[nbr] = weight
def str(self): return str(self.id) + ' connectedTo: '
def getConnections(self): return self.connectedTo.keys()
def getId(self): return self.id
def getWeight(self,nbr): return self.connectedTo[nbr]
def getColor(self): return self.color
def setColor(self, newColor): self.color = newColor
def getPred(self): return self.predecessor
def setPred(self, newPred): self.predecessor = newPred
def getDiscovery(self): return self.discovery
def setDiscovery(self, newDiscovery): self.discovery = newDiscovery
def getFinish(self): return self.Finish
def setFinish(self, newFinish): self.finish = newFinish
def getDistance(self): return self.distance
def setDistance(self, newDistance): self.distance = newDistance
""" File: graph.py """ from vertex import Vertex
class Graph: def init(self): self.vertList = {} self.numVertices = 0
def addVertex(self,key): self.numVertices = self.numVertices + 1 newVertex = Vertex(key) self.vertList[key] = newVertex return newVertex
def getVertex(self,n): if n in self.vertList: return self.vertList[n] else: return None
def contains(self,n): return n in self.vertList
def addEdge(self,f,t,cost=0): if f not in self.vertList: nv = self.addVertex(f) if t not in self.vertList: nv = self.addVertex(t) self.vertList[f].addNeighbor
(self.vertList[t], cost)
def getVertices(self): return self.vertList.keys()
def iter(self): return iter(self.vertList.values())
0 1
3
2
4