Graphs - Data Structures - Lab, Exercises of Data Structures and Algorithms

Some concept of Data Structures are Data Structures, Dynamic Programming, First-In-First-Out, Implementation, Python Code. Main points of this lecture are: Graphs, Word-Ladder Puzzle, Transforms, Changing, Letter, Algorithm, Graph, Represented, Connect, Edges

Typology: Exercises

2012/2013
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 04/30/2013

naji
naji 🇮🇳

4.3

(6)

87 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives: To understand how a graph can be represented and traversed.
To start the lab: Download and unzip the file lab11.zip
Part A: In a word-ladder puzzle (discussed in class) transforms one word into another by changing one letter at a
time, e.g., transform FOOL into SAGE by
FOOL FOIL FAIL FALL PALL PALE SALE SAGE
.
We used a graph algorithm to solve this problem by constructing a graph such that
words are represented by the vertices, and
edges connect vertices containing words that differ by only one letter
a) For the words listed below, complete the graph by adding edges as defined above.
foul
fool
foil
fail fall
pall
cool pool
poll
pole
pope
pale
sale
page
sage
b) To find the shortest transformation from FOOL to SAGE, why did we decide on using a Breadth First Search
(BFS) traveral (i.e., where you find all vertices a distance 1 (directly connected) from FOOL, before finding all
vertices a distance 2 from FOOL, etc) instead of a Depth-First Search (DFS) traversal?
c) Run the lab11/word_ladder_BFS.py program. Examine the “enqueue” and “dequeue” lines of output
produced by the bfs(g,g.getVertex("fool")) call. Does this output match the expected “enqueues” and
“dequeues” performed during a bfs of the above graph starting at “fool”?
d) The bfs algorithm sets the value of each vertex’s predecessor to point to the vertex object that enqueued
it. Add code to the end of the word_ladder_BFS.py program that traverses the “linked list” of
predecessor references from “sage” to “fool.” and prints the corresponding word ladder from “fool” to “sage.”
After you have answered the above questions and completed the code, raise your hand and explain your
answers.
Data Structures Lab 11 Graphs Name:____________
Lab11 - 1
Docsity.com
pf2
Discount

On special offer

Partial preview of the text

Download Graphs - Data Structures - Lab and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Objectives: To understand how a graph can be represented and traversed.

To start the lab: Download and unzip the file lab11.zip

Part A: In a word-ladder puzzle (discussed in class) transforms one word into another by changing one letter at a time, e.g., transform FOOL into SAGE by FOOL → FOIL → FAIL → FALL → PALL → PALE → SALE → SAGE.

We used a graph algorithm to solve this problem by constructing a graph such that  words are represented by the vertices, and  edges connect vertices containing words that differ by only one letter

a) For the words listed below, complete the graph by adding edges as defined above.

foul

fool

foil

fail fall

pall

cool pool

poll

pole

pope

pale

sale

page

sage

b) To find the shortest transformation from FOOL to SAGE, why did we decide on using a Breadth First Search (BFS) traveral (i.e., where you find all vertices a distance 1 (directly connected) from FOOL, before finding all vertices a distance 2 from FOOL, etc) instead of a Depth-First Search (DFS) traversal?

c) Run the lab11/word_ladder_BFS.py program. Examine the “enqueue” and “dequeue” lines of output produced by the bfs(g,g.getVertex("fool")) call. Does this output match the expected “enqueues” and “dequeues” performed during a bfs of the above graph starting at “fool”?

d) The bfs algorithm sets the value of each vertex’s predecessor to point to the vertex object that enqueued it. Add code to the end of the word_ladder_BFS.py program that traverses the “linked list” of predecessor references from “sage” to “fool.” and prints the corresponding word ladder from “fool” to “sage.”

After you have answered the above questions and completed the code, raise your hand and explain your answers.

Data Structures Lab 11 Graphs Name:____________

Lab11 - 1

Docsity.com

Part B: Section 7.5 uses recursion and the run-time stack to implement a DFS traversal. The DFSGraph uses a time attribute to note when a vertex if first encountered (discovery attribute) in the depth-first search and when a vertex in backtracked through (finish attribute). Consider the graph for making pancakes where vertices are steps and edges represents the partial order among the steps.

3/4 cup "milk"

1 "egg"

1 Tbl "oil"

1 cup "flour"

"mix" the batter "pour" 1/2 cup

"heat" griddle

"turn" when bubbly

heat "syrup" "eat"

of batter

1/

a) Run the lab11/made_pancake_DFS.py program. Write on the above graph the discovery and finish attributes (e.g., 1 / 12 of “oil”) assigned to each vertex by executing the dfs method..

b) A topological sort algorithm can use the dfs finish attributes to determine a proper order to avoid putting the "cart before the horse." For example, we don't want to "pour ½ cup of batter" before we "mix the batter", and we don't want to "mix the batter" until all the ingredients have been added. Outline the steps to perform a topological sort from the finish attributes.

After you have answered the above questions, raise your hand and explain your answers.

EXTRA CREDIT:

Add code to the end of the made_pancake_DFS.py program to print the topological sort for making pancakes.

Data Structures Lab 11 Graphs Name:____________

Lab11 - 2

from graph import Graph class DFSGraph(Graph): def init(self): super().init() self.time = 0 def dfs(self): for aVertex in self: aVertex.setColor('white') aVertex.setPred(-1) for aVertex in self: if aVertex.getColor() == 'white': self.dfsvisit(aVertex) def dfsvisit(self,startVertex): startVertex.setColor('gray') self.time += 1 startVertex.setDiscovery(self.time) for nextVertex in startVertex.getConnections(): if nextVertex.getColor() == 'white': nextVertex.setPred(startVertex) self.dfsvisit(nextVertex) startVertex.setColor('black') self.time += 1 startVertex.setFinish(self.time)

Docsity.com