150 likes | 270 Views
Assignment 2 Graphs and Dynamic Programming. Objectives. To implement in Java, a weighted graph ADT To implement a depth-first traversal of the graph To implement a dynamic programming solution to the shortest path problem using Dijkstra ’ s algorithm. Description.
E N D
Assignment 2 Graphs and Dynamic Programming
Objectives • To implement in Java, a weighted graph ADT • To implement a depth-first traversal of the graph • To implement a dynamic programming solution to the shortest path problem using Dijkstra’s algorithm.
Description • adjacency list data structure • a menu that provides the user various options: • build a graph • perform a depth-first traversal of the graph • find the shortest path in the graph using Dijkstra’s algorithm (http://java.sun.com/docs/books/tutorial/uiswing/components/index.html http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html )
Technique • Build a graph • perform a depth-first traversal of the grap • find the shortest path in the graph using Dijkstra’s algorithm
M 12000 1300 2800 L P 2400 1500 s A 300 Example
Build a graph • input from standard input or a file or redirection • The number of vertices in the graph • Vertex information (one vertex per line) • The number of edges in the graph • Edge information (one edge per line).
Build a graph • Input.txt • 5 //number of vertices • M • L • P • A • S • 6 //number of edges • M L 1200 • M P 1300 • M A 2800 • L S 1500 • P S 2400 • A S 300
Build a graph • adjacency list : • implemented as a table • star representation • linked list • implemented as a matrix • adjacency matrix • incidence matrix
S S M S L M P P L M A S L M P 1300 2400 1200 1300 1200 2800 1500 1500 300 2400 A A NULL NULL NULL 2800 300 NUll NUll Build a graph
DFS (1) create a boolean array visited[1..n], initialize all values to false except for visited[v] to true (2) call DFS(v) to visit all nodes reachable via a path function DFS(v) for each neighboring nodes w of v do if visited[w] is false then set visited[w] to true; call DFS(w) // recursive call
if starting vertex is M, the access sequence is M -> L -> S -> P ->A M 12000 1300 2800 L P 2400 1500 s A 300 DFS
Dijkstra’s Shortest Path Algorithm • Dijkstra (weighted simple digraph, vertex first) • for all vertices v • currDist(v) = ; • currDist(first) = 0; • tobeChecked = all vertices; • while tobeChecked is not empty • v = a vertex in tobeChecked with minimal currDist(v); • remove v from tobeChecked; • for all vertices u adjacent to vand in tobeChecked • if currDist(u) > currDist(v) + weight(edge(vu)) • currDist(u) = currDist(v) + weight(edge(vu)); • predecessor(u) = v;
M 1200 1300 2800 L P 2400 1500 s A 300 Dijkstra’s Shortest Path Algorithm
Dijkstra’s Shortest Path Algorithm • http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/DijkstraApp.shtml?demo1
Input Specification: Menu driven program – assume error free input provided by user. • Output Specification: See the specifications above for the format of the output for each type of operation performed by the application. Deliverables: • Source code file (.java) and compiled version (.class). • Submit these via WebCT by the deadline of 11:55 PM Sunday July 10th.