⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 Specification File adjacencyListGragh class GeneralGraph: use adjacency list to implement the gr
💻 JAVA
字号:
/*
Accessor methods:
* endVertices(e): an array of the two endvertices of e
* opposite(v, e): the vertex opposite of v on e
* areAdjacent(v, w): true iff v and w are adjacent
* replace(v, x): replace element at vertex v with x
* replace(e, x): replace element at edge e with x

Update methods :
* insertVertex(o): insert a vertex storing element o
* insertEdge(v, w, o): insert an edge (v,w) storing element o
* removeVertex(v): remove vertex v (and its incident edges)
* removeEdge(e): remove edge e

Iterator methods:
* incidentEdges(v): edges incident to v
* vertices(): all vertices in the graph
* edges(): all edges in the graph
*/
package adjacencyListGragh;
import java.util.Vector;

public class Test {
	
	public static void main(String[] args) 
	{
		GeneralGraph graph = new GeneralGraph(true);
		
		Vertex a = graph.insertVertex(1);
		Vertex b = graph.insertVertex(2);
		Vertex c = graph.insertVertex(3);
		Vertex d = graph.insertVertex(4);
		Vertex e = graph.insertVertex(5);
		Vertex f = graph.insertVertex(6);
		Vertex g = graph.insertVertex(7);
		Vertex h = graph.insertVertex(8);
		
		Edge A = graph.insertEdge(a , b, 12);
		Edge B = graph.insertEdge(c , d, 34);
		Edge C = graph.insertEdge(e , f, 56);
		Edge D = graph.insertEdge(g , g, 77);
		Edge E = graph.insertEdge(a , g, 17);
		Edge F = graph.insertEdge(d , a, 41);
		Edge G = graph.insertEdge(c , b, 32);
		Edge H = graph.insertEdge(g , f, 76);
		//A.printed=false;B.printed=false;C.printed=false;D.printed=false;E.printed=false;F.printed=false;G.printed=false;H.printed=false;
		
		System.out.println("All the vertices are listed:");
		printVertex(graph.vertices());
		
		System.out.println("\nAll the edge are listed:");
		printEdge(graph.edges());

		System.out.println("\nAll the edge incident to vertex a is listed:");
		printEdge(graph.incidentEdges(a));

		System.out.println("\nEnd vertices of Edge A:");
		Vertex[] endVertices = graph.endVertices(A);
		for (int i= 0; i< endVertices.length; i++)
			System.out.print(endVertices[i].getElement()+"\t");
		
		System.out.println("\nOpposite vertices of vertex a in Edge A:");
		Vertex oppositeVertex = graph.opposite(a, A);
			System.out.print(oppositeVertex.getElement()+"\t");
			
		System.out.println("\nDecide whether two vertex are Adjacent:");
		boolean bool1 = graph.areAdjacent(a , b);
		boolean bool2 = graph.areAdjacent(b , a);
		boolean bool3 = graph.areAdjacent(c , f);
			System.out.print(bool1+"\t"+bool2+"\t"+bool3+"\t");
			
		System.out.println("\nReplace the element of vertex h with 80:");
		graph.replace(h, 80);
		printVertex(graph.vertices());
		
		graph.replace(G, 320);
		System.out.println("\nReplace the element of edge G with 320:");
		printEdge(graph.edges());
			
		graph.removeEdge(B);
		System.out.println("\nAfter remove the edge B, all the edge are listed:");
		printEdge(graph.edges());
		System.out.println("\nAfter remove the edge B, all the vertices are listed:");
		printVertex(graph.vertices());
		
		graph.removeVertex(b);
		System.out.println("\nAfter remove the vertex b, all the edge are listed:");
		printEdge(graph.edges());
		System.out.println("\nAfter remove the vertex b, all the vertices are listed:");
		printVertex(graph.vertices());
		
		
	}
	public static void printVertex(Vector<Vertex> vertex){
		for (int i= 0; i< vertex.size(); i++)
			System.out.print(vertex.get(i).getElement()+"\t");
	}
	public static void printEdge(Vector<Edge> edge){
		for (int i= 0; i< edge.size(); i++)
			System.out.print(edge.get(i).getElement()+"\t");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -