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

📄 graph.java

📁 Java实现的常用数据结构算法
💻 JAVA
字号:
package graph;
 class Graph implements Cloneable{
	public boolean[][] edges;
	public Object[] labels;
	public Graph(int n){
		edges=new boolean[n][n];
		labels=new Object[n];
		unitLine(false);
	}
	public void setLabels(Object[] obj){
		int len=labels.length;
		for(int i=0;i<len;i++){
			labels[i]=obj[i];
		}
	}
	public void unitLine(boolean value){
		int len=labels.length;
		for(int i=0;i<len;i++){
			for(int j=0;j<len;j++)
			edges[i][j]=value;
		}
	}
	public void setEdges(boolean[][] ed){
		edges=ed;
	}
	public void addEdge(int source ,int target){
		edges[source][target]=true;
	}
	public void delEdge(int source ,int target){
		edges[source][target]=false;
	}
	
	public Object clone(){
		Graph answer;
		try{
			answer=(Graph)super.clone();
		}
		catch(CloneNotSupportedException e)
		{
			throw new RuntimeException
			("this class does not implement Clonesable");
		}
		answer.edges=(boolean[][])edges.clone();
		answer.labels=(boolean[][])labels.clone();
		return answer;
	}
	public Object getLabel(int vertex){
		return labels[vertex];
	}
	public boolean isEdge(int source ,int target){
		return edges[source][target];
	}
	public int[] neighbors(int vertex){
		int i;
		int count;
		int[] answer;
		count=0;
		for( i=0;i<labels.length;i++){
			if(edges[vertex][i])
			count++;
		}
		answer=new int[count];
		count=0;
		for( i=0;i<labels.length;i++){
			if(edges[vertex][i])
			answer[count++]=i;
		}
		return answer;
	}
	public void removeEdge(int source,int target){
		edges[source][target]=false;
	}
	public void setLabel(int vertex,Object newLabel)
	{
		labels[vertex]=newLabel; 
	}
	public int size(){
		return labels.length;
	}

}

⌨️ 快捷键说明

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