Docsity

Prim's Algorithm and Minimum Spanning Tree, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1. Suppose you had a map of settlements on the planet X
(Assume edges could connecting all vertices with their Euclidean distances as their costs)
a
b
d
h
e
f
g
c
We want to build roads that allow us to travel between any pair of cities. Because resources are scarce, we want
the total length of all roads build to be minimal. Since all cities will be connected anyway, it does not matter
where we start, but assume we start at “a”.
a) Assuming we start at city “a” which city would you connect first? Why this city?
b) What city would you connect next to expand your partial road network?
c) What would be some characteristics of the resulting "graph" after all the cities are connected?
d) Does your algorithm come up with the overall best (globally optimal) result?
Data Structures (CS 1520) Lecture 28 Name:_________________
Lecture 28 Page 1
Docsity.com
pf3

Partial preview of the text

Download Prim's Algorithm and Minimum Spanning Tree and more Study notes Data Structures and Algorithms in PDF only on Docsity!

1. Suppose you had a map of settlements on the planet X

(Assume edges could connecting all vertices with their Euclidean distances as their costs)

a

b

d

h

e

f

g

c

We want to build roads that allow us to travel between any pair of cities. Because resources are scarce, we want

the total length of all roads build to be minimal. Since all cities will be connected anyway, it does not matter

where we start, but assume we start at “a”.

a) Assuming we start at city “a” which city would you connect first? Why this city?

b) What city would you connect next to expand your partial road network?

c) What would be some characteristics of the resulting "graph" after all the cities are connected?

d) Does your algorithm come up with the overall best (globally optimal) result?

Lecture 28 Page 1

2. Prim’s algorithm for determining the minimum-spanning tree of a graph is another example of a greedy

algorithm. Unlike divide-and-conquer and dynamic programming algorithms, greedy algorithms DO 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).

a) What greedy criteria does Prim’s algorithm use to select the next vertex and edge to the partial minimum

spanning tree?

b) Consider the textbook’s Prim’s Algorithm code (Listing 7.12 p. 346) which is incorrect.

def prim(G,start):

pq = PriorityQueue()

for v in G:

v.setDistance(sys.maxsize)

v.setPred(None)

start.setDistance(0)

pq.buildHeap([(v.getDistance(),v) for v in G])

while not pq.isEmpty():

currentVert = pq.delMin()

for nextVert in currentVert.getConnections():

newCost = currentVert.getWeight(nextVert) \

+ currentVert.getDistance()

if v in pq and newCost<nextVert.getDistance():

nextVert.setPred(currentVert)

nextVert.setDistance(newCost)

pq.decreaseKey(nextVert,newCost)

c) What is wrong with the code? (Fix the above code.)

3. To avoid “massive” changes to the binHeap class, it can store PriorityQueueEntry objects:

a) Update the above Prim’s algorithm code to use PriorityQueueEntry objects.

b) Why do the lt and gt methods compare key attributes, but eq compare val attributes?

Lecture 28 Page 2

class PriorityQueueEntry: def init(self,x,y): self.key = x self.val = y

def getKey(self): return self.key

def getValue(self): return self.val

def setValue(self, newValue): self.val = newValue

def lt(self,other): return self.key < other.key

def gt(self,other): return self.key > other.key

def eq(self, other): return self.val == other.val

def hash(self): return self.key