CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
8<br />
13 // Construct an empty graph<br />
14 <strong>Weighted</strong>Graph();<br />
15<br />
16 // Construct a graph from vertices <strong>and</strong> edges objects<br />
17 <strong>Weighted</strong>Graph(vector& vertices, vector& edges);<br />
18<br />
19 // Construct a graph with vertices 0, 1, ..., n-1 <strong>and</strong><br />
20 // edges in a vector<br />
21 <strong>Weighted</strong>Graph(int numberOfVertices, vector& edges);<br />
22<br />
23 // Print all edges in the weighted tree<br />
24 void <strong>Weighted</strong>Graph::print<strong>Weighted</strong>Edges();<br />
<strong>25</strong><br />
26 // Add a weighted edge<br />
27 bool addEdge(int u, int v, double w);<br />
28<br />
29 // Get a minimum spanning tree rooted at vertex 0<br />
30 MST getMinimumSpanningTree();<br />
31<br />
32 // Get a minimum spanning tree rooted at a specified vertex<br />
33 MST getMinimumSpanningTree(int startingVertex);<br />
34<br />
35 // Find single-source shortest paths<br />
36 ShortestPathTree getShortestPath(int sourceVertex);<br />
37 };<br />
38<br />
39 const int INFINITY = 2147483647;<br />
40<br />
41 template<br />
42 <strong>Weighted</strong>Graph::<strong>Weighted</strong>Graph()<br />
43 {<br />
44 }<br />
45<br />
46 template<br />
47 <strong>Weighted</strong>Graph::<strong>Weighted</strong>Graph(vector& vertices,<br />
48 vector& edges)<br />
49 {<br />
50 // Add vertices to the graph<br />
51 for (unsigned i = 0; i < vertices.size(); i++)<br />
52 {<br />
53 addVertex(vertices[i]);<br />
54 }<br />
55<br />
56 // Add edges to the graph<br />
57 for (unsigned i = 0; i < edges.size(); i++)<br />
58 {<br />
59 addEdge(edges[i].u, edges[i].v, edges[i].weight);<br />
60 }<br />
61 }<br />
62<br />
63 template<br />
64 <strong>Weighted</strong>Graph::<strong>Weighted</strong>Graph(int numberOfVertices,<br />
65 vector& edges)<br />
66 {<br />
67 // Add vertices to the graph<br />
68 for (int i = 0; i < numberOfVertices; i++)<br />
69 addVertex(i); // vertices is {0, 1, 2, ..., n-1}<br />
70<br />
71 // Add edges to the graph