Gaurav DS Worksheet 4
Gaurav DS Worksheet 4
Gaurav DS Worksheet 4
WORKSHEET 4
1. Aim: Consider a directed graph where the weight of its edges can be one of x, 2x or
3x (x is a positive integer). Efficiently compute the least-cost path from source to
destination.
2. Source Code:
#include <iostream>
using namespace std;
class graph {
public:
graph *e0, *e1 , *e2 , *e3 , *e4;
int wt0, wt1 , wt2 , wt3 , wt4;
};
int main(){
graph g0,g1,g2,g3,g4;
g0.e0 = NULL; g0.wt0 = 0;
g0.e1 = &g1; g0.wt1 = 3;
g0.e2 = NULL; g0.wt2 = 0;
g0.e3 = NULL; g0.wt3 = 0;
g0.e4 = &g4; g0.wt4 = 1;
g1.e0 = NULL; g1.wt0 = 0;
g1.e1 = NULL; g1.wt1 = 0;
g1.e2 = &g2; g1.wt2 = 1;
g1.e3 = &g3; g1.wt3 = 3;
g1.e4 = &g4; g1.wt4 = 1;
g2.e0 = NULL; g2.wt0 = 0;
g2.e1 = NULL; g2.wt1 = 0;
g2.e4 = NULL; g2.wt4 = 0;
g2.e3 = NULL; g2.wt3 = 0;
g2.e2 = NULL; g2.wt2 = 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Screenshot of Outputs:
4. Learning Outcomes
(i) I now understand what a graph in a data structure is.
(ii) I now know how to use classes to generate graphs.
(iii) I now understand the idea behind data structures' weighted graphs.
(iv) I now know how to use a weighted graph to determine the shortest path between two
nodes.
(v) I now understand how to traverse a graph's nodes.