import java.util.ArrayList;
import java.util.List;
public class GraphReverse{
protected SeqList<E> vertexlist;
protected int[][] adjmatrix;
private final int MAX_WEIGHT = Integer.MAX_VALUE / 2;
public AdjMatrixGraph(int n) {
this.vertexlist = new SeqList<E>(n);
this.adjmatrix = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
this.adjmatrix[i][j] = (i == j) ? 0 : MAX_WEIGHT;
}
public AdjMatrixGraph(E[] vertices, Edge[] edges) {
this(vertices.length);
for (int i = 0; i < vertices.length; i++)
insertVertex(vertices[i]);
for (int j = 0; j < edges.length; j++)
insertEdge(edges[j]);
}
public AdjMatrixGraph(SeqList<E> list, Edge[] edges) {
this(list.length());
this.vertexlist = list;
for (int j = 0; j < edges.length; j++)
insertEdge(edges[j]);
}
public int vertexCount() {
return this.vertexlist.length();
}
public E get(int i) {
return this.vertexlist.get(i);
}
public boolean insertVertex(E vertex) {
return this.vertexlist.add(vertex);
}
public boolean insertEdge(int i, int j, int weight)
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()&& i != j && adjmatrix[i][j] == MAX_WEIGHT) {
this.adjmatrix[i][j] = weight;
return true;
}
return false;
}
public boolean insertEdge(Edge edge) {
if (edge != null);
return insertEdge(edge.start, edge.dest, edge.weight);
}
public String toString() {
String str = "顶点集合: " + vertexlist.toString() + "\n";
str += "邻近矩阵: \n";
int n = vertexCount();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (adjmatrix[i][j] == MAX_WEIGHT)
str += " ∞";
else
str += " " + adjmatrix[i][j];
}
str += "\n";
}
return str;
}
public boolean removeEdge(int i, int j)
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()
&& i != j && this.adjmatrix[i][j] != MAX_WEIGHT) {
this.adjmatrix[i][j] = MAX_WEIGHT;
return true;
}
return false;
}
public boolean removeVertex(int v)
{
int n = vertexCount();
if (v >= 0 && v < n) {
this.vertexlist.remove(v);
for (int i = v; i < n - 1; i++)
for (int j = 0; j < n; j++)
this.adjmatrix[i][j] = this.adjmatrix[i + 1][j];
for (int j = v; j < n - 1; j++)
for (int i = 0; i < n - 1; i++)
this.adjmatrix[i][j] = this.adjmatrix[i][j + 1];
return true;
}
return false;
}
public int getFirstNeighbor(int v)
{
return getNextNeighbor(v, -1);
}
public int getNextNeighbor(int v, int w) {
if (v >= 0 && v < vertexCount() && w >= -1 && w < vertexCount()
&& v != w)
for (int j = w + 1; j < vertexCount(); j++)
if (adjmatrix[v][j] > 0 && adjmatrix[v][j] < MAX_WEIGHT)
return j;
return -1;
}
public void DFStraverse() {
int n = this.vertexCount();
boolean[] visited = new boolean[n];
for (int i = 1; i < n; i++) {
visited[i] = false;
}
for (int j = 0; j < n; j++) {
if (!visited[j]) {
System.out.println("以该顶点为" + j + "起始点的遍历:");
this.DFS(j, visited);
}
}
}
public void DFS(int v, boolean[] visited2) {
boolean[] visited = visited2;
visited[v] = true;
System.out.println("遍历顶点" + v);
for (int w = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w)) {
if (!visited[w]) {
visited[w] = true;
DFS(w, visited);
}
}
}
public void BFStraverse() {
int n = this.vertexCount();
boolean[] visited = new boolean[n];
MyQueue myqueue = new MyQueue();
for (int i = 1; i < n; i++) {
visited[i] = false;
}
for (int j = 0; j < n; j++) {
if (!visited[j]) {
visited[j] = true;
System.out.println("遍历起点:" + j);
myqueue.EnQueue(j);
while (!myqueue.empty()) {
int v = (Integer) myqueue.DeQueue();
System.out.println("遍历点:" + v);
for (int w = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w)) {
if (!visited[w]) {
visited[w] = true;
myqueue.EnQueue(w);
}
}
}
}
}
}
public void topologicalSort() {
int n = this.vertexCount();
int[] indegree = new int[n];
MyStack mystack = new MyStack();
String route = "拓扑排序出发:";
int count = 0;
for (int i = 0; i < n; i++) {
indegree[i] = 0;
for (int j = 0; j < n; j++) {
if (adjmatrix[j][i] != 0 && adjmatrix[j][i] != MAX_WEIGHT) {
indegree[i] += 1;
}
}
if (indegree[i] == 0) {
mystack.push(i);
}
}
while (!mystack.empty()) {
int v = (Integer) mystack.pop();
route += "->" + v;
++count;
for (int w = this.getFirstNeighbor(v); w >= 0; w = this .getNextNeighbor(v, w)) {
indegree[w] -= 1;
if (indegree[w] == 0) {
mystack.push(w);
}
}
}
if (count < n) {
System.out.println("存在回路,不满足拓扑排序的条件");
}
else {
System.out.println("实现拓扑排序" + route);
}
}
}
import java.util.List;
public class GraphReverse{
protected SeqList<E> vertexlist;
protected int[][] adjmatrix;
private final int MAX_WEIGHT = Integer.MAX_VALUE / 2;
public AdjMatrixGraph(int n) {
this.vertexlist = new SeqList<E>(n);
this.adjmatrix = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
this.adjmatrix[i][j] = (i == j) ? 0 : MAX_WEIGHT;
}
public AdjMatrixGraph(E[] vertices, Edge[] edges) {
this(vertices.length);
for (int i = 0; i < vertices.length; i++)
insertVertex(vertices[i]);
for (int j = 0; j < edges.length; j++)
insertEdge(edges[j]);
}
public AdjMatrixGraph(SeqList<E> list, Edge[] edges) {
this(list.length());
this.vertexlist = list;
for (int j = 0; j < edges.length; j++)
insertEdge(edges[j]);
}
public int vertexCount() {
return this.vertexlist.length();
}
public E get(int i) {
return this.vertexlist.get(i);
}
public boolean insertVertex(E vertex) {
return this.vertexlist.add(vertex);
}
public boolean insertEdge(int i, int j, int weight)
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()&& i != j && adjmatrix[i][j] == MAX_WEIGHT) {
this.adjmatrix[i][j] = weight;
return true;
}
return false;
}
public boolean insertEdge(Edge edge) {
if (edge != null);
return insertEdge(edge.start, edge.dest, edge.weight);
}
public String toString() {
String str = "顶点集合: " + vertexlist.toString() + "\n";
str += "邻近矩阵: \n";
int n = vertexCount();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (adjmatrix[i][j] == MAX_WEIGHT)
str += " ∞";
else
str += " " + adjmatrix[i][j];
}
str += "\n";
}
return str;
}
public boolean removeEdge(int i, int j)
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()
&& i != j && this.adjmatrix[i][j] != MAX_WEIGHT) {
this.adjmatrix[i][j] = MAX_WEIGHT;
return true;
}
return false;
}
public boolean removeVertex(int v)
{
int n = vertexCount();
if (v >= 0 && v < n) {
this.vertexlist.remove(v);
for (int i = v; i < n - 1; i++)
for (int j = 0; j < n; j++)
this.adjmatrix[i][j] = this.adjmatrix[i + 1][j];
for (int j = v; j < n - 1; j++)
for (int i = 0; i < n - 1; i++)
this.adjmatrix[i][j] = this.adjmatrix[i][j + 1];
return true;
}
return false;
}
public int getFirstNeighbor(int v)
{
return getNextNeighbor(v, -1);
}
public int getNextNeighbor(int v, int w) {
if (v >= 0 && v < vertexCount() && w >= -1 && w < vertexCount()
&& v != w)
for (int j = w + 1; j < vertexCount(); j++)
if (adjmatrix[v][j] > 0 && adjmatrix[v][j] < MAX_WEIGHT)
return j;
return -1;
}
public void DFStraverse() {
int n = this.vertexCount();
boolean[] visited = new boolean[n];
for (int i = 1; i < n; i++) {
visited[i] = false;
}
for (int j = 0; j < n; j++) {
if (!visited[j]) {
System.out.println("以该顶点为" + j + "起始点的遍历:");
this.DFS(j, visited);
}
}
}
public void DFS(int v, boolean[] visited2) {
boolean[] visited = visited2;
visited[v] = true;
System.out.println("遍历顶点" + v);
for (int w = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w)) {
if (!visited[w]) {
visited[w] = true;
DFS(w, visited);
}
}
}
public void BFStraverse() {
int n = this.vertexCount();
boolean[] visited = new boolean[n];
MyQueue myqueue = new MyQueue();
for (int i = 1; i < n; i++) {
visited[i] = false;
}
for (int j = 0; j < n; j++) {
if (!visited[j]) {
visited[j] = true;
System.out.println("遍历起点:" + j);
myqueue.EnQueue(j);
while (!myqueue.empty()) {
int v = (Integer) myqueue.DeQueue();
System.out.println("遍历点:" + v);
for (int w = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w)) {
if (!visited[w]) {
visited[w] = true;
myqueue.EnQueue(w);
}
}
}
}
}
}
public void topologicalSort() {
int n = this.vertexCount();
int[] indegree = new int[n];
MyStack mystack = new MyStack();
String route = "拓扑排序出发:";
int count = 0;
for (int i = 0; i < n; i++) {
indegree[i] = 0;
for (int j = 0; j < n; j++) {
if (adjmatrix[j][i] != 0 && adjmatrix[j][i] != MAX_WEIGHT) {
indegree[i] += 1;
}
}
if (indegree[i] == 0) {
mystack.push(i);
}
}
while (!mystack.empty()) {
int v = (Integer) mystack.pop();
route += "->" + v;
++count;
for (int w = this.getFirstNeighbor(v); w >= 0; w = this .getNextNeighbor(v, w)) {
indegree[w] -= 1;
if (indegree[w] == 0) {
mystack.push(w);
}
}
}
if (count < n) {
System.out.println("存在回路,不满足拓扑排序的条件");
}
else {
System.out.println("实现拓扑排序" + route);
}
}
}