从图中的文本文件中读取节点数

时间:2015-12-04 03:50:28

标签: java graph nodes filereader vertices

我想要读取文本文件中只包含边数的节点数。我不想在我的文本文件的顶部添加读取顶点的数量。这是文本文件中包含的内容。

11 3
2 3
0 3
1 4
5 4
5 7
6 7
7 8
8 9
9 10
0 5

问题是我无法从读取文件中获取节点数。我在想是否找到节点的最大值,如果以0开头则添加1.但是我仍然无法得到它所以我尝试通过读取nextInt并与另一个nextInt进行比较。这就是我的意思和到目前为止所做的工作。

public static int readNode(String mazeFile) {
    int numNode = 0;
    File mf = new File(mazeFile);
    try {
        Scanner scan = new Scanner(mf);
        int arc = readLineCount(mf);
        for (int i = 0; i < arc; i++) {
            while (scan.hasNext()) {
                int n1 = scan.nextInt();
                int n2 = scan.nextInt();
                if (n1 > n2) {
                    n2 = n1;
                    numNode = n2;
                } else if (n1 < n2) {
                    n1 = n2;
                    numNode = n1;
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return numNode;
}

我需要改变什么吗?

1 个答案:

答案 0 :(得分:2)

使用此方法,您仍然指望用户为图表提供连续的整数节点编号。如果有人在图表中只有42个节点时选择节点号1111111111,该怎么办?要解决此问题,请将节点编号视为符号。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

class Graph {
  final Set<Integer> nodes = new HashSet<>();
  final Map<Integer, Set<Integer>> edges = new HashMap<>();

  static class Reader {
    String fileName;
    Graph graph;

    Reader(String fileName) {
      this.fileName = fileName;
    }

    Graph read() {
      try {
        return scanGraph();
      } catch (FileNotFoundException ignore) {
        return new Graph();
      }
    }

    Graph scanGraph() throws FileNotFoundException {
      graph = new Graph();
      Scanner scanner = new Scanner(new File(fileName));
      while (scanner.hasNextInt()) {
        int n1 = scanner.nextInt();
        int n2 = scanner.nextInt();
        graph.nodes.add(n1);
        graph.nodes.add(n2);
        addDirectedEdge(n1, n2);
      }
      return graph;
    }

    void addDirectedEdge(Integer n1, Integer n2) {
      if (graph.edges.containsKey(n1)) {
        graph.edges.get(n1).add(n2);
      } else {
        Set<Integer> to = new HashSet<>();
        to.add(n2);
        graph.edges.put(n1, to);
      }
    }    
  }

  interface Visitor {
    void visit(int node);
  }    

  void visitDepthFirst(int start, Visitor visitor) {
    visitDepthFirst(start, visitor, new HashSet<>());
  }

  void visitDepthFirst(int node, Visitor visitor, Set<Integer> visited) {
    visitor.visit(node);
    visited.add(node);
    Set<Integer> successors = edges.get(node);
    if (successors == null) {
      return;
    }
    for (int successor : successors) {
      if (!visited.contains(successor)) {
        visitDepthFirst(successor, visitor, visited);
      }
    }
  }

  public static void main(String [] args) {
    Graph graph = new Graph.Reader(args[0]).read();
    System.out.println("The graph has " + graph.nodes.size() + " nodes:");
    System.out.println(graph.nodes);
    System.out.println("Adjacency list:");
    System.out.println(graph.edges);
    System.out.println("A preorder depth first visit starting from 0:");
    graph.visitDepthFirst(0, new Visitor() {
      @Override
      public void visit(int node) {
        System.out.println("Visiting " + node);
      }
    });
  }
}

我已经在您的代码中修复了其他一些可疑的做法。但我还没有使用Java 8的功能,这会使这个问题变得不那么冗长。

输出:

The graph has 12 nodes:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Adjacency list:
{0=[3, 5], 1=[4], 2=[3], 5=[4, 7], 6=[7], 7=[8], 8=[9], 9=[10], 11=[3]}
A preorder depth first visit starting from 0:
Visiting 0
Visiting 3
Visiting 5
Visiting 4
Visiting 7
Visiting 8
Visiting 9
Visiting 10

为了简洁起见,我省略了生产Java的细节,如公共/私有和getter / setter。