-1

有人可以澄清这部分代码如何将数据输入到图表中。.data 是什么类型的文件?

public class DepthFirst {
static int numNodes;
static GraphNode nodes[];

  ...........
  ...........



public static void readGraph() throws IOException {
    File input = new File("digraph.data");
    Scanner digraph = new Scanner(input).useDelimiter("\\D+");

            //if file is not empty
    if (digraph.hasNext()) {
        int nodeNum;
        numNodes = digraph.nextInt(); //System.out.println(numNodes);
        nodes = new GraphNode[numNodes];
        while (digraph.hasNext()) {
            nodeNum = digraph.nextInt();
            nodes[nodeNum] = new GraphNode(nodeNum, digraph.nextInt());
            for (int i = 0; i < nodes[nodeNum].getDegree(); i++) 
                nodes[nodeNum].setAdjListIndex(digraph.nextInt(), i); 
        } 
    }
    else return; 
} 

干杯工作

4

1 回答 1

0

该文件很可能只是一个普通的旧文本文件。.data扩展名没有特别的意义。

如果您运行的是 Windows,请在任何文本编辑器中打开它,例如记事本或写字板。它应该是非常可读的。

据我所知:

该文件由一堆整数组成,由 0-9 范围以外的任何整数分隔(这\\D+意味着,它是一个正则表达式,更多信息请参见此内容),可能是空格。

第一项是节点数。其次是所有节点。

对于每个节点,都有一个节点索引,然后是它的度数,然后是连接顶点的索引(计数 = 度数)。

有关如何准确构建图的更多详细信息,我们需要更多关于GraphNode外观的详细信息,但是一旦您阅读了一本不错的 Java 书籍或参加了不错的 Java 编程课程,就应该很清楚了。

于 2013-05-07T12:18:30.440 回答