Lecture Exercises Programming Exercises

J.E.D.I the shortest path in a weighted, directed graph.

6.8 Summary

• A graph G= V,E consists of a finite, non-empty set of vertices and a set of edges • Adjacency matrix representation of graphs requires On2 space. Telling if there is an edge i, j takes O1 time • In adjacency list representation of graphs, a list is maintained such that for any vertex i in G, LISTi points to the list of vertices adjacent from i • DFS traverses the graph as deeply as possible while BFS traverses the graph as broadly as possible • The minimun cost spanning tree problem can be solved using Prims algorithm or Kruskals algorithm • Dijkstras algorithm is for the SSSP problem while Floyds algorithm is for the APSP problem

6.9 Lecture Exercises

1. What is the DFS and BFS listing of elements of the following graphs with 1 as the start vertex? a b 2. Find the minimum cost spanning tree of the following graphs using Kruskal’s Data Structures 122 J.E.D.I algorithm and Prim’s algorithm. Give the cost of the MST. a b 3. Solve for the SSSP problem of the following graphs using Dijkstras algorithm. Show the value, class and path of the vertices for each iteration: a DIJKSTRA 1 Source: 8, Destination: 4 Data Structures 123 J.E.D.I b DIJKSTRA 2 Source: 1, Destination: 7 4. Solve the APSP of the following graphs by giving the matrices A and Path using Floyds algorithm:

6.10 Programming Exercises

1. Create a Java class definition for weighted directed graphs using adjacency matrix representation. 2. Implement the two graph traversal algorithms. 3. Shortest Path Using Dijkstra’s Algorithm. Implement a shortest path adviser given a map and cost of each connection. The program shall ask for an input file containing the sources or destinations vertices and the connections between the locations edges along with the cost. The locations are of the form number, location where number is an integer assigned to a vertex and location is the name of the vertex. Every number, location pair must be placed on a separate line in the input file. To terminate the input, use 0, 0. Connections shall be retrieved also from the same file. An edge definition must be of the form i, j, k, one line per edge, where i is the number assigned to the source, j the number assigned to the destination and k the cost of reaching j from i Remember, we use directed graphs here. Terminate the input using 0, 0, 0. After the input, create a map and show it to the user. Ask the user for the source and destination and give the shortest path by using Dijkstra’s algorithm. Data Structures 124 J.E.D.I Show the output on the screen. The output consists of the path and its cost. The path must be of the form: source  location 2  …  location n-1  destination Sample Input File 1, Math Building 2, Science Building 3, Engineering . . 0, 0 1, 2, 10 1, 3, 5 3, 2, 2 . . 0, 0, 0  start of edge definition Sample Program Flow [ MAP and LEGEND ] Input Source: 1 Input Destination: 2 Source: Math Building Destination: Science Building Path: Math Building  Engineering  Science Building Cost: 7 Data Structures 125 J.E.D.I 7 Lists

7.1 Objectives