我正在尝试雇用
compile group: 'org.jgrapht', name: 'jgrapht-core', version: '1.1.0'
可视化团队和团队成员之间的关系。
例如:-
“第一个团队”有成员“ A成员”,“ B成员”,“ C成员”
“第二小组”有成员“ C小组成员”,“ D小组成员”
所以我添加了以下顶点
final Graph<Displayable, DefaultEdge> directedGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
directedGraph.addVertex(team_0001);
directedGraph.addVertex(team_0002);
directedGraph.addVertex(teamMember_A);
directedGraph.addVertex(teamMember_B);
directedGraph.addVertex(teamMember_C);
directedGraph.addVertex(teamMember_D);
directedGraph.addEdge(team_0001, teamMember_A);
directedGraph.addEdge(team_0001, teamMember_B);
directedGraph.addEdge(team_0001, teamMember_C);
directedGraph.addEdge(team_0002, teamMember_C);
directedGraph.addEdge(team_0002, teamMember_D);
当我遍历此图时,我想要以下输出:-
Team_0001
teamMember_A
teamMember_B
teamMember_C
Team_0002
teamMember_C
teamMember_D
我该如何实现?
我必须开发一个客户图迭代器吗?
最佳答案
根据定义,您的图形是两部分的:{分区1 =团队顶点,分区2 =玩家顶点}。
因此,只需维护一个Team顶点列表,然后为每个顶点调用Graphs.neighborListOf(.)
:List<Displayable> teams=...;for(Displayable v : teams) System.out.println("Team"+v+" has the following members: "+Graphs.neighborListOf(teamGraph,v);
另外,由于图形是有向的,因此可以遍历边缘:for(DefaultEdge e : teamGraph.edgeSet()) System.out.println(String.format("Player %s belongs to team %s",teamGraph.getEdgeTarget(e), teamGraph.getEdgeSource(e)));