您的位置:首页 > 其它

第五周作业——有向图邻接表表示及反向图构造

2014-04-10 19:55 387 查看
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

;    

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
;   

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
;   

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
;    

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);   

}    

}      

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: