Depth First Search - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. There are two general approaches for traversing a graph from some starting vertex s:
Depth First Search (DFS) where you explore as deeply into the graph as possible. If you reach a “dead end,” we
backtrack to the deepest vertex that allows us to try a different path.
Breadth First Search (BFS) where you find all vertices a distance 1 (directly connected) from s, before finding all
vertices a distance 2 from s, etc.
What data structure would be helpful in each type of search? Why?
a) Breadth First Search (BFS):
b) Depth First Search (DFS):
2. On the next page is the textbook’s edge, vertex, and graph implementations.
a) How does this graph implementation maintain its set of vertices?
b) How does this graph implementation maintain its set of edges?
3. Assuming a graph g containing the word-ladder graph from lecture 26, on the diagram trace the bfs algorithm by
showing the value of each vertex’s color, predecessor, and distance attributes?
Data Structures (CS 1520) Lecture 27 Name:_________________
Lecture 27 Page 1
Docsity.com
pf3
pf4

Partial preview of the text

Download Depth First Search - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

1. There are two general approaches for traversing a graph from some starting vertex s :

 Depth First Search (DFS) where you explore as deeply into the graph as possible. If you reach a “dead end,” we

backtrack to the deepest vertex that allows us to try a different path.

 Breadth First Search (BFS) where you find all vertices a distance 1 (directly connected) from s, before finding all

vertices a distance 2 from s, etc.

What data structure would be helpful in each type of search? Why?

a) Breadth First Search (BFS):

b) Depth First Search (DFS):

2. On the next page is the textbook’s edge, vertex, and graph implementations.

a) How does this graph implementation maintain its set of vertices?

b) How does this graph implementation maintain its set of edges?

3. Assuming a graph g containing the word-ladder graph from lecture 26, on the diagram trace the bfs algorithm by

showing the value of each vertex’s color, predecessor, and distance attributes?

Lecture 27 Page 1

Lecture 27 Page 2

""" 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: '

  • str([x.id for x in self.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())

5. Consider the following directed graph (diagraph).

Dijkstra’s Algorithm is a greedy algorithm that finds the shortest path from

some vertex, say v 0 , to all other vertices. A greedy algorithm, unlike

divide-and-conquer and dynamic programming algorithms, DOES NOT divide

a problem into smaller subproblems. Instead a greedy algorithm builds a

solution by making a sequence of choices that look best ("locally" optimal) at

the moment without regard for past or future choices (no backtracking to fix

bad choices). Dijkstra’s algorithm builds a subgraph by repeatedly selecting

the next closest vertex to v 0 that is not already in the subgraph. Initially, only

vertex v 0 is in the subgraph with a distance of 0 from itself.

a) What would be the order of vertices added to the subgraph during Dijkstra’s algorithm?

v 0 ,

b) What greedy criteria did you use to select the next vertex to add to the subgraph?

c) What data structure could be used to efficiently determine that selection?

d) How might this data structure need to be modified?

Lecture 27 Page 4

0 1

3

2

4

v v

v

v

v