예제 #1
0
  public FriendshipGraph(String graphFile) throws FileNotFoundException {
    graph = new UndirGraph<Friend>();
    ArrayList<Friend> friends = new ArrayList<Friend>();
    Scanner scfile = null;

    try {
      scfile = new Scanner(new File(graphFile));
    } catch (FileNotFoundException e) {
      throw new FileNotFoundException();
    }

    int NumOfVert = Integer.parseInt(scfile.nextLine());

    for (int i = 0; i < NumOfVert; i++) {
      StringTokenizer txt = new StringTokenizer(scfile.nextLine(), "|");
      String name = txt.nextToken(), college;

      if (txt.nextToken().equals("y")) college = txt.nextToken();
      else college = "";

      graph.addVertex(new Friend(name, college));
      friends.add(new Friend(name, college));
    }

    while (scfile.hasNext()) {
      String line = scfile.next(),
          nameOne = line.substring(0, line.indexOf('|')),
          nameTwo = line.substring(line.indexOf('|') + 1);

      int friendOne = 0, friendTwo = 0;

      for (int i = 0; i < friends.size(); i++) {
        if (friends.get(i).name.equals(nameOne)) friendOne = i;
        else if (friends.get(i).name.equals(nameTwo)) friendTwo = i;
      }

      graph.addEdge(friendOne, new Neighbor(friendTwo));
    }

    scfile.close();
  }
예제 #2
0
  public FriendshipGraph<Friend> getStudentsAtTheCollege(String college) {

    UndirGraph<Friend> collegeSubgraph = new UndirGraph<Friend>();
    int vCount = this.graph.numberOfVertices();

    for (int i = 0; i < vCount; i++) {
      if (this.graph.vertexInfoOf(i).school.compareTo(college) == 0)
        collegeSubgraph.addVertex(this.graph.vertexInfoOf(i));
    }

    vCount = collegeSubgraph.numberOfVertices();

    for (int i = 0; i < vCount; i++) {
      int firstRootRef = this.graph.vertexNumberOf(collegeSubgraph.vertexInfoOf(i));

      for (int j = 0; j < vCount; j++) {
        int secondRootRef = this.graph.vertexNumberOf(collegeSubgraph.vertexInfoOf(j));

        if (this.graph.containsEdge(firstRootRef, new Neighbor(secondRootRef)))
          collegeSubgraph.addEdge(i, new Neighbor(j));
      }
    }
    return new FriendshipGraph<Friend>(collegeSubgraph);
  }