`
guoyiqi
  • 浏览: 1018631 次
社区版块
存档分类
最新评论

java 数据结构最短路径

 
阅读更多

木其工作室 QQ 928900200

 

COMP3712/8712 GE    COMPUTER PROGRAMMING 3    ASSIGNMENT 2 
Assignment 2: Shortest Paths in Graphs 
 
Write a Java program that implements  Dijkstra's  algorithm to find shortest paths to all vertices  in a
directed weighted graph  from a given  starting vertex.  
As mentioned  in the lectures,  choosing an implementation  for the data structure  representing  a
graph  is highly dependent  on the sparsity of the graph. For dense graphs,  an adjacency matrix
representation  is preferable, but  for sparse graphs,  an adjacency list representation may be more
efficient. You are asked to implement  either  the adjacency  list or adjacency matrix representation
for graphs  in your program.   If you choose to extend you implementation  from Lab03 then  it will
need account for weighted edges.
Your program  should  (a) read in data defining  a graph  from a text  file, (b) build an adjacency list OR
matrix  representation  of the weighted graph and (c) use the graph  representation  to find shortest
paths from a specified vertex  in the graph  to all other vertices, using Dijkstra’s algorithm*. 
In addition,  you should  (d) provide documentation for your program describing  how to run  it, and
explaining  how the code works. You should also justify your choice of data structures*,  and details of
your code implementation,  in terms of space and  time efficiency. 
*(e) Use both a standard  List and Java’s builtin  PriorityQueue  for the storing  the list of distances
from the given  starting  vertex to other vertices compare the performance  (e.g. runtime, memory,
etc). 
(f) Implement  your own Priority Queue  class using  a Heap as described  in lectures and tutorials.
Compare your implementation  with the List and built-in PriorityQueue  compare.  Implementing  your
own priority queue  should allow you to make optimizations  specific to the Dijkstra’s Algorithm  (e.g.
not being generic)  that the Java PriorityQueue  must maintain  to be widely usable.
 
Marking Guide
It is expected  that all students  attempt parts (a)-(f).  All students  should be able to complete parts
(a)-(d) and most should be able to complete part  (e).  Part (f) is potentially more challenging.  The
table below gives an indication as to the distribution  of marks  to the parts of the assignment
described  above.
 
 
 
 
 
  
Part  Weight (100%)  Weight (15%)
a  5  2
b  20  3
c  25  3
d  20  3
e  20  3
f  10  2
Total  100  15 COMP3712/8712 GE    COMPUTER PROGRAMMING 3    ASSIGNMENT 2 
 
Data Format: There are five example  files (in Assignment02-data.zip) provided as example
input  files. Feel free to test your program with your own files as well. The data format is as follows:  
The first line  contains the number  of vertices.  
Every line after that represents  all edges going out from a particular vertex. The first number  on each
line is the number  of the vertex. After that,  there are a number  of pairs of integers. The first integer
is the index of a vertex connected to the first vertex in the line. The second integer  is the weight of
the edge  connecting  those two vertices.  
For example,  if the text file content  is: 

1 2 18 3 12 
2 1 22 
3 2 16 4 14 4 1 31  
then  this means that there are 4 vertices, numbered  1 to 4. Vertex 1 has an edge to vertex 2, with
weight 18, and an edge  to vertex 3 with weight 12. Vertex 2 has an edge back to vertex 1, but its
weight  is 22 (because  this is a directed graph,  if there is an edge  from A to B, it doesn’t mean  that
there  is an edge from B to A, or if there  is, the weights don’t need to be the same).  
 
Output: The graph  in graphPosLittle.txt has size of 10 vertices. If coded correctly, a call to find
shortest paths from vertex 1 should produce  the following output:  
Shortest path to 1: 1: cost = 0 
Shortest path to 2: 1 2: cost = 6 
Shortest path to 3: 1 7 9 10 4 8 5 3: cost = 35 
Shortest path to 4: 1 7 9 10 4: cost = 21 
Shortest path to 5: 1 7 9 10 4 8 5: cost = 28 
Shortest path to 6: 1 7 9 10 4 8 5 3 6: cost = 37 
Shortest path to 7: 1 7: cost = 6 
Shortest path to 8: 1 7 9 10 4 8: cost = 25 
Shortest path to 9: 1 7 9: cost = 11 
Shortest path to 10: 1 7 9 10: cost = 15 
 
Submission of Assignment
Submission will be via FLO.  A single  zip file containing  all source code and required  documents.    If
there are any special requirements  for running  the program  these should be detailed  somewhere,
preferably  in separate document about running  your program.   Additional  documents  should be in
PDF format.  If you have utilised  additional data files for your assignment  and they are too large  to
upload  to FLO then provide a link in your documentation. 

分享到:
评论

相关推荐

    java实现的求迷宫最短路径算法

    总之,这个Java实现的迷宫最短路径算法项目是一个很好的学习资源,它涵盖了数据结构(如坐标位置类)、路径搜索算法以及如何在Java中编写清晰的代码。通过研究这些代码,开发者可以提升自己的算法理解和编程技能。

    java实现的最短路径问题

    在Java实现迪杰斯特拉算法时,我们可能需要以下数据结构: - `Graph`类:表示图,包含节点和边。 - `Node`类:表示图中的节点,存储节点的值和到起点的距离。 - `Edge`类:表示图中的边,包含两个节点和权重。 - `...

    Floyd最短路径java实现

    运行上述Java程序并输出结果后,检查每个顶点对之间的最短路径是否正确。 总结,Floyd最短路径算法通过动态规划的思想,逐步增加中间节点,逐步完善最短路径信息。在Java中实现时,主要利用了二维数组来存储图的...

    用Java编写的最短路径代码

    本篇文章将深入探讨如何使用Java编程语言解决最短路径问题,并结合提供的文件"最短路径"进行分析。 首先,我们要了解几种常见的最短路径算法,包括Dijkstra算法、Bellman-Ford算法以及Floyd-Warshall算法。其中,...

    用java通过文件操作实现最短路径问题

    读取这些信息后,构建图的数据结构,如邻接矩阵或邻接表。 2. **处理数据**:根据所选的最短路径算法处理图数据,计算最短路径。 3. **写入输出文件**:使用`PrintWriter`类将计算结果写入新的文件。输出可能包括...

    数据结构之最短路径java实现

    本话题将聚焦于数据结构中的“最短路径”问题,并探讨如何使用Java语言来实现这一功能。最短路径问题广泛应用于网络路由、地图导航、物流配送等领域,其核心在于寻找两个节点之间的最小成本路径。 首先,我们需要...

    数据结构Java版图3最短路径ppt课件.ppt

    总结起来,"数据结构Java版图3最短路径"的学习主要涵盖Dijkstra算法的应用,理解并实现该算法能够帮助开发者有效地解决实际问题,例如在路由选择、网络优化、地图导航等领域寻找最短路径。通过深入学习和实践,可以...

    数据结构课程设计最短路径

    2. 算法实现:Dijkstra算法或Floyd-Warshall算法的Java代码实现,包括数据结构的初始化、路径更新、节点状态管理等。 3. 输入与输出:读取图的结构信息,可能来自于文件或用户输入,然后输出最短路径结果。 4. 错误...

    用java写的查询某市地铁的最短路径,递归算法

    通过理解递归、最短路径算法以及数据结构,我们可以深入探讨和改进这样的系统,使其更加高效和适应不同的需求。在实际应用中,类似的方法也可以应用于其他领域,如道路网络导航或社交网络分析。

    基于gdal 最短路径计算

    压缩包中的"RoadGraph"可能是一个数据结构或者类,用于表示道路网络的图模型。它可能包含了节点、边以及它们之间的连接信息,以及一些辅助方法用于进行最短路径计算。 总的来说,实现基于GDAL的最短路径计算,需要...

    图的结构建立与最短路径算法

    图的结构建立与最短路径算法是图论中两个核心概念,主要应用于网络优化、路径规划等领域。本文将深入探讨这两个主题。 首先,我们来理解什么是图的结构建立。图是由顶点(或节点)和边(或弧)构成的数据结构,用来...

    最短路径-Dijkstra-欧洲旅行 数据结构实验

    在本数据结构实验中,我们将探索“最短路径-Dijkstra-欧洲旅行”的概念。这个实验是基于Dijkstra算法,一种广泛用于寻找图中两点间最短路径的算法。在这个特定的场景下,我们假设有一个覆盖整个欧洲的铁路系统,每个...

    最短路径(Java)

    总结来说,用Java实现Dijkstra算法主要涉及数据结构的选择(如优先队列、哈希表等)以及算法的具体步骤实现。通过这个算法,我们可以有效地找到图中的最短路径,这对于网络路由、图形遍历等多种问题都有广泛的应用。...

    数据结构—图及其应用(交通问题,实现最短路径、最短时间、最少费用查询).rar

    本主题聚焦于数据结构中的"图",以及如何利用图解决实际问题,如交通路线的最短路径、最短时间和最少费用查询。我们将探讨图的基本概念、图的表示方法、Dijkstra算法、Floyd-Warshall算法,以及如何在C语言中实现...

    java 具有图形界面的最短路径问题的求解

    在计算机科学领域,最短路径问题是一个经典的图论问题,其目标是找到网络中的两个...实际的代码实现会涉及数据结构、算法和用户交互等多个方面的知识,这对于提升Java编程能力和理解图论问题的解决方法都非常有帮助。

    图-最短路径-可视化.zip

    本项目以"图-最短路径-可视化.zip"为主题,利用Java编程语言实现了一个图形用户界面(GUI)来演示如何找出图中任意两点间的最短路径。 首先,我们要了解图的基本概念。图是由顶点(节点)和边(连接节点的线)构成...

    图的最短路径.xls

    最短路径问题是图论研究中的一个经典算法问题, 旨在寻找图(由结点和路径组成的)中两结点之间的最短路径。 算法具体的形式包括: 确定起点的最短路径问题 - 即已知起始结点,求最短路径的问题。 [1] 确定终点的...

    有向图最短路径(空间分析、数据结构)

    有向图最短路径是图论中的一个经典问题,它在计算机科学,特别是算法和数据结构领域具有重要的应用。在给定的标题和描述中,我们可以看出这是一个使用C++面向对象编程实现的程序,用于解决有向图中最短路径的问题。...

    最短路径(单源多源等形式).rar

    在这个压缩包中,包含的文件主要围绕两种算法来解决这一问题:单源最短路径算法Dijkstra和多源最短路径算法Floyd-Warshall,以及它们在不同数据结构上的实现。 1. Dijkstra算法:由荷兰计算机科学家艾兹格·迪科斯...

Global site tag (gtag.js) - Google Analytics