1 / 15

Assignment 2 Graphs and Dynamic Programming

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.

kail
Download Presentation

Assignment 2 Graphs and Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment 2 Graphs and Dynamic Programming

  2. 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.

  3. 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 )

  4. Technique • Build a graph • perform a depth-first traversal of the grap • find the shortest path in the graph using Dijkstra’s algorithm

  5. M 12000 1300 2800 L P 2400 1500 s A 300 Example

  6. 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).

  7. 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

  8. Build a graph • adjacency list : • implemented as a table • star representation • linked list • implemented as a matrix • adjacency matrix • incidence matrix

  9. 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

  10. 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

  11. 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

  12. 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;

  13. M 1200 1300 2800 L P 2400 1500 s A 300 Dijkstra’s Shortest Path Algorithm

  14. Dijkstra’s Shortest Path Algorithm • http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/DijkstraApp.shtml?demo1

  15. 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.

More Related