0% found this document useful (0 votes)
62 views9 pages

cs3329 Assignment

Kruskal's and Prim's algorithms were used to find the minimum spanning tree for a weighted graph. Kruskal's algorithm selected edges in order of ascending weight without creating cycles. Prim's algorithm started from a node and iteratively added the smallest weighted edge connecting unvisited nodes. Bellman-Ford and Dijkstra's algorithms were applied to find the shortest paths between nodes in other weighted graphs, tracking distances and edges at each step. Depth-first search and breadth-first search traversals of a sample graph were also demonstrated. Finally, a dynamic programming approach was provided to solve a modified rod-cutting problem that includes a fixed cost for each cut.

Uploaded by

Ngọc Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views9 pages

cs3329 Assignment

Kruskal's and Prim's algorithms were used to find the minimum spanning tree for a weighted graph. Kruskal's algorithm selected edges in order of ascending weight without creating cycles. Prim's algorithm started from a node and iteratively added the smallest weighted edge connecting unvisited nodes. Bellman-Ford and Dijkstra's algorithms were applied to find the shortest paths between nodes in other weighted graphs, tracking distances and edges at each step. Depth-first search and breadth-first search traversals of a sample graph were also demonstrated. Finally, a dynamic programming approach was provided to solve a modified rod-cutting problem that includes a fixed cost for each cut.

Uploaded by

Ngọc Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name : Chu Hoàng Hải Long

ID:1596879

1. [ 5+5] Use Kruskal's algorithm to find a minimal spanning tree for the weighted graph shown below,
showing steps. Then, use Prim's algorithm to do the same thing, using the same weighted graph, again
showing steps.

Kruskal algorithm
To start, we’ll be picking the smallest edge. In this case would be AB with the smallest edge weight of 2.
We repeatly look for the smallest edges that don’t create cycle. In this case edge CD with the edge weight
of 3 was chosen. The next are EF(4), CG(5), EG(7), and finally AF(8).

Prim’s algorithm
First we will create a empty list X = {}; This list will be use to keep track on nodes we have touched

First we will chose a nodes. We will chose node A. The list is now X ={A}

We’ll then examine all vertices reachable from A then choose the smallest edges that connect to an
unvisited node ( AB ). X = {A;B}

We’ll now look at all nodes that is reachable from A and B and continue picking the smallest edges that
connect to an unvisited nodes. In this case we will pick AF with only the edge weight of 8. X = {A;B;F}

Continue the process. We will pick unvisited node FE with the smallest edge weight of 4. X={A;B;F;E}

Next will be EG with the edge weight of 7. X={A;B;F;E;G}

Next will be GC with the edge weight of 5. X={A;B;F;E;G;C}

Finally we will chose CD with the edge weight of 3. X={A;B;F;E;G;C;D}

All nodes are now visited and connected to a tree.

2. Use Bellman-Ford algorithm to find single source shortest path for the weighted graph shown
below, showing all the steps

There are 5 vertices. That’s mean we’ll have to do 4 iteration.


We’ll set 0 as the distance of nodes 1.
We’ll look at every nodes and exam it’s outgoing edge.
We will not update the table if it’s edge don’t improve the distance

1st iteration
Nodes 1 2 3 4 5
Value 0 6 4 7 2

2nd iteration

Nodes 1 2 3 4 5
Value 0 2 4 7 2
3rd iteration

Nodes 1 2 3 4 5
Value 0 2 4 7 -2

4th iteration

All the value are the same as the 3rd iteration

Nodes 1 2 3 4 5
Value 0 2 4 7 -2
Shortest path

3. Use Dijkstra algorithm to find shortest path between source and destination for the weighted
graph shown below, showing all the steps

We will chose the shortest path from one node to every other.
First set A = 0;
We can now reach C with the cost of 1 and D with the cost of 2 so we’ll pick C.
C = 1 => B = 1 +2 = 3 => F= B + 3 = 6;
D = 1 + 1 = 2;
E = 1 + 3 = 4; => F = E + 2 = 6;
We will pick D with the lowest edge weight.

D = 2; => G = 2 + 1 = 3
We will pick G with the lowest edge weight.
G = 3 => F = 3 + 1 = 4;
We will pick F with the lowest edge weight

The shortest path is : A-> C -> D - > G -> F.

4. Starting from node A, find DFS and BFS for the graph shown below. Show all your steps.
Breadth-first search

- Enqueue: A
- Enqueue: B-> C -> D
- Dequeue: A
- Enqueue : E -> F
- Dequeue: B -> C -> D
- Dequeue: E -> F

Breadth-first search will follow this search order : A -> B -> C -> D -> E -> F.

Depth-first search

- Push A into stack


- Pop A from stack
- Push B, C, D into stack
- Pop B from stack.
- Push E, F into stack
- Pop E,F from stack
- Pop C from stack
- Pop D from stack

Depth-first search will follow this search order A -> B -> E -> F -> C -> D

5. [10] Consider a modification of the rod-cutting problem in which, in addition to a price pi for each
rod, each cut incurs a fixed cost c. The revenue associated with a solution is now the sum of the prices of
the pieces minus the cost of making the cuts. Give a dynamic programming algorithm in pseudo code
format to solve this modified problem

Implement a global array r[0…n]


Rodcut(n)
if n <= 0
return r[0]
else
for i = 1 to n
if r[n-1] >= 0
max = pi + r[n-i]
else
max = pi + Rodcut(n-1)

if max > q

q = max

r[n] = q

return q

You might also like