简体   繁体   English

使用Dijkstra算法从加权图形文本文件创建矩阵的最佳方法?

[英]Best way to create matrix from a weighted graph text file using Dijkstra's Algorithm?

I have been researching Graph theory and Dijsktra's only to find myself with too many ways to go about this, and I'm not sure which to apply to this specific problem. 我一直在研究图论和Dijsktra,却发现自己有太多解决此问题的方法,而且我不确定该解决特定问题的方法。 Here are the requirements: 要求如下:

In centralized routing, all the routing information is generated and maintained in a centralized location. 在集中式路由中,所有路由信息都生成并维护在一个集中位置。 A common way of maintaining routing information centrally is via a routing matrix. 集中维护路由信息的常用方法是通过路由矩阵。 A routing matrix has a row and a column for each node in the network, where a row corresponds to the source node and a column corresponds to the destination node. 路由矩阵对于网络中的每个节点都有一行和一列,其中一行对应于源节点,而列对应于目标节点。 The entry in row i column j is a pair (x , y), where x is the first node on the shortest path from i to j, and y is the cost of the shortest path from i to j. 第i行j列中的条目是一对(x,y),其中x是从i到j的最短路径上的第一个节点,而y是从i到j的最短路径的成本。

Write a program that reads a weighted graph representing a network, and finds and outputs the corresponding routing matrix. 编写一个程序,读取表示网络的加权图,并找到并输出相应的路由矩阵。 The routing matrix will be written both to the screen and to an output file. 路由矩阵将同时写入屏幕和输出文件。 Use Dijkstra's algorithm to find the shortest cost and path between nodes. 使用Dijkstra的算法查找节点之间的最短成本和路径。 The program runs from the command line with two optional command line arguments. 该程序从命令行运行,带有两个可选的命令行参数。 Use the following command line options to indicate the presence of a command line argument: -i (for input file name) and –o (for output file name). 使用以下命令行选项来指示命令行参数的存在:-i(用于输入文件名)和-o(用于输出文件名)。 If no command line arguments are present, the program uses “xy_input.txt” and “xy_output.txt” as default input and output file names respectively. 如果没有命令行参数,则程序分别使用“ xy_input.txt”和“ xy_output.txt”作为默认输入和输出文件名。 See examples below: 请参阅以下示例:

  • java xy_rmatrix java xy_rmatrix
  • java xy_rmatrix –i xy_rmatrixi.txt –o xy-rmatrixo.txt java xy_rmatrix –i xy_rmatrixi.txt –o xy-rmatrixo.txt

Sample Input/Output: The input file contains zero or more lines representing a graph of a network. 输入/输出样本:输入文件包含零个或多个代表网络图形的线。 Each line represents a bi-directional edge that is made up of two vertices (routers) and the cost associated with the link (communication line) between them. 每条线表示一个双向边,该边由两个顶点(路由器)和与它们之间的链接(通信线)关联的成本组成。 One or more whitespaces (blank, tab, etc.) will separate data on each line and the node names might be a string rather than a single character. 一个或多个空格(空白,制表符等)将分隔每一行上的数据,并且节点名称可能是字符串而不是单个字符。 Note that the routing matrix rows and columns are listed in alphabetical order. 请注意,路由矩阵的行和列以字母顺序列出。

Input: 输入:

https://i.stack.imgur.com/08zpC.png

Output: 输出:

在此处输入图片说明

My main question is, for this particular problem is it necessary to store the contents of the input file in its own data structure such as a graph or adjacency list, and how would I go about implementing these in Java with vertices/nodes and edges in a way that I can perform Dijsktra's Algorithm? 我的主要问题是,对于此特定问题,有必要将输入文件的内容存储在其自己的数据结构中,例如图形或邻接列表,以及如何在Java中使用顶点/节点和边来实现这些内容?一种执行Dijsktra算法的方法?

Am I also correct to assume that the routing matrix specified is synonymous with an adjacency matrix? 我是否还正确地假设指定的路由矩阵与邻接矩阵同义?

Note: I am not fishing for code, just a step in the right direction. 注意:我不是在寻找代码,只是朝着正确方向迈出了一步。

I think in your case, the routing matrix could be represented by a two-dimensional array. 我认为在您的情况下,路由矩阵可以用二维数组表示。 Dijkstra's algorithm finds the shortest path from one source to any destination. Dijkstra的算法可找到从一个来源到任何目的地的最短路径。 You can use 'A' as the source first, and then 'B', and so on to get the routing matrix from each point to every other point, as required. 您可以根据需要首先使用“ A”作为源,然后使用“ B”,依此类推,以获得从每个点到每个其他点的路由矩阵。 You could use adjacency-lists or arrays again to represent the graph information in order to perform dijkstra from each source. 您可以再次使用邻接列表或数组来表示图信息,以便从每个源执行dijkstra。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM