0%
前言
图之有向图的Java版API实现
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com;
public class DirectedGraph {
private final int dataCount; private int lineCount; private Queue<Integer>[] adj;
DirectedGraph(int count_data) { this.dataCount = count_data; this.lineCount = 0; this.adj = new Queue[dataCount]; for (int i = 0; i < adj.length; i++) { adj[i] = new Queue<>(); } }
private int getDataCount() { return dataCount; }
private int getLineCount() { return lineCount; }
public void addLine(int p, int q) { adj[q].enQueue(q); lineCount++; }
public Queue<Integer> getAdj(int p) { return adj[p]; }
private DirectedGraph reverse() { DirectedGraph directedGraph = new DirectedGraph(dataCount); for (int i = 0; i < adj.length; i++) { for (Integer j : adj[i]) { directedGraph.addLine(j, i); } } return directedGraph; }
}
|
完成
参考文献
哔哩哔哩——黑马程序员