各种图的各种操作

数据结构课程设计-----图的创建及相关操作的实现

输入图的类型(有向图、有向网、无向图、无向网)、图的顶点个数、边的条数、图的顶点信息、各条边以及边的权重(如果是网),任意选用一种数据结构,编写程序将图存入内存,并实现以下的各个操作:

  1. 存储结构的转换:如果是无向图或无向网,在邻接矩阵、邻接表、邻接多重表之间转换;如果是有向图或有向网,在邻接矩阵、邻接表和逆邻接表、十字链表之间转换;

  2. 完成增加顶点和删除顶点的功能,删除顶点也要删除与之关联的边;

  3. 完成增加边和删除边的功能;

  4. 完成图的深度优先遍历和广度优先遍历;

  5. 求图的深度优先或广度优先的生成树(或生成森林)(存储结构为孩子-兄弟链表),并对生成树进行遍历;

  6. 判断图的连通性,输出连通分量的个数;

  7. 判断图中是否存在环;

  8. 判断uv是否存在路径;

  9. 对于图(不是网),求顶点uv的一条简单路径;

  10. 对于图(不是网),求顶点uv的所有简单路径;

  11. 实现DijkstraFloyd算法求最短路径;

  12. 实现普里姆或克鲁斯卡尔算法求最小生成树。

package GraphDemo;

public class Vertex<AnyType>{
	public AnyType data;
	public Arc firstArc;
	public boolean vis;
	public boolean primVis;
	public int inDegree;
	public int outDegree;
	public int degree;
	public int topNum;
	public int dist;
	public int primDist;
	public int weight;
	public Vertex(AnyType data,Arc firstArc){
		this.data=data;
		this.firstArc=firstArc;
	}
	public Vertex(AnyType data){
		this(data,null);
	}
}

package GraphDemo;

import TwoWayLinkedListDemo.TwoWayLinkedListNode;

public class Arc{
	int vex;
	int adjVex;
	public Arc nextArc;
	int weight;
	public Arc(){}
	public Arc(int adjVex,Arc nextArc,int weight){
		this.adjVex=adjVex;
		this.nextArc=nextArc;
		this.weight=weight;
	}
	public Arc(int adjVex,int weight){
		this(adjVex,null,weight);
	}
	public Arc(int adjVex,Arc nextArc){
		this(adjVex,nextArc,1);
	}
	public Arc(int adjVex){
		this(adjVex,null,1);
	}
}



package GraphDemo;

public class Node<AnyType>{
	AnyType data;
	Node<AnyType> firstChild,Sibling;
	Node(){
		data=null;
		firstChild=Sibling=null;
	}
	Node(AnyType data){
		this.data=data;
		firstChild=Sibling=null;
	}
	Node(AnyType data,Node<AnyType>lt,Node<AnyType>rt){
		this.data=data;
		firstChild=lt;
		Sibling=rt;
	}
	public Node<AnyType>getleftChild(){
		return firstChild;
	}
	public Node<AnyType>getrigthChild(){
		return Sibling;
	}
}
package GraphDemo;

class DisjSets{
	public int s[];
	public DisjSets(int numElements){
		s=new int[numElements];
		for(int i=0;i<s.length;i++)
			s[i]=i;
	}
	public void union(int root1,int root2){
		int x,y;
		x=find(root1);
		y=find(root2);
		if(x!=y) s[y]=x;
	}
	public int find(int x){
		while(x!=s[x])
			x=s[x];
		return x;
	}
}

package GraphDemo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Graph<AnyType>{
	int DG=1,UNG=2,DN=3,UND=4;
	int vexNum;
	int arcNum;
	int type;
	int count;
	int INF=0xfffff;
	Node first;
	LinkedList<Vertex> vertexs=new LinkedList<Vertex>();
	public Graph(int type,int vexNum,int arcNum){
		this.type=type;
		this.arcNum=arcNum;
		this.vexNum=vexNum;
	}
	//创建图
	public Graph(){}
	//创建有向图
	public void createDG(List<AnyType>v,List<Integer>a,List<Integer> b,List<Integer> x){
		for(int i=0;i<vexNum;i++)
			vertexs.add(new Vertex<AnyType>(v.get(i)));
		for(int i=0;i<a.size();i++){
			int arc1=a.get(i);
			int arc2=b.get(i);
			Vertex<AnyType> ver=vertexs.get(arc1);
			Arc arc=new Arc(arc2);
			arc.vex=arc1;
			arc.weight=x.get(i);
			arc.nextArc=ver.firstArc;
			ver.firstArc=arc;
		}
	}
	//创建无向图
	public void createUDG(List<AnyType> v,List<Integer> a,List<Integer> b,List<Integer> x){
		for(int i=0;i<vexNum;i++)
			vertexs.add(new Vertex<AnyType>(v.get(i)));
		for(int i=0;i<a.size();i++){
			int arc1=a.get(i);
			int arc2=b.get(i);
			Vertex<AnyType> ver=vertexs.get(arc1);
			Arc p=new Arc(arc2);
			p.nextArc=ver.firstArc;
			ver.firstArc=p;
			Arc q=new Arc(arc1);
			ver=vertexs.get(arc2);
			q.nextArc=ver.firstArc;
			ver.firstArc=q;
			q.weight=x.get(i);
			p.weight=x.get(i);
		}
	}
	//创建有向网
	public void createDN(List<AnyType> v,List<Integer> a,List<Integer> b,List<Integer> x){
		for(int i=0;i<vexNum;i++)
			vertexs.add(new Vertex<AnyType>(v.get(i)));
		for(int i=0;i<a.size();i++){
	     int arc1=a.get(i);
	     int arc2=b.get(i);	
	     int weight=x.get(i);
		 Vertex<AnyType> ver=vertexs.get(arc1);
		 Arc arc=new Arc(arc2,weight);
		 arc.nextArc=ver.firstArc;
		 ver.firstArc=arc; 
		}
    }
	//创建无向网
    public void createUDN(List<AnyType> v,List<Integer> a,List<Integer> b,List<Integer> x){
		for(int i=0;i<vexNum;i++)	
			vertexs.add(new Vertex<AnyType>(v.get(i)));
		for(int i=0;i<a.size();i++){
	     	int arc1=a.get(i);
	     	int arc2=b.get(i);	
		 Vertex<AnyType> ver=vertexs.get(arc1);
		 Arc p=new Arc(arc2);
		 p.nextArc=ver.firstArc;
		 ver.firstArc=p;
		 Arc q=new Arc(arc1);
		 ver=vertexs.get(arc2);
		 q.nextArc=ver.firstArc;
		 ver.firstArc=q;
		 q.weight=x.get(i);
		 p.weight=x.get(i);
		}
    }
    public void createGraph(int type,List<AnyType> v,List<Integer> a,List<Integer> b,List<Integer> x){
    	if(vexNum<=0||arcNum<0){
    		System.out.println("您创建的图有误!");
    		return;
    	}
    	switch(type){
    	case 1:createDG(v,a,b,x);break;
    	case 2:createUDG(v,a,b,x);break;
    	case 3:createDN(v,a,b,x);break;
    	case 4:createUDN(v,a,b,x);break;
    	default:System.out.println("您指定的图的类型有误!");
    	}
    }
    //求第一个邻接点
    public int firstAdjVex(int v){
    	if(v<0||v>vexNum){
    		System.out.println("输入不合法!");
    	}
    	Vertex<AnyType> ver=vertexs.get(v);
    	Arc p=ver.firstArc;
    	if(p!=null)return p.adjVex;
    	return -1;
    }
    //求下一条边
    public int nextAdjVex(int v,int w){
    	if(v<0||v>=vexNum){
    		System.out.println("输入不合法!");
    	}
    	Vertex<AnyType> ver=vertexs.get(v);
    	Arc arc=ver.firstArc;
    	int next=arc.adjVex;
    	while(arc!=null&&next!=w){
    		arc=arc.nextArc;
    		next=arc.adjVex;
    	}
    	if(arc!=null)
    		arc=arc.nextArc;
    	if(arc!=null)return arc.adjVex;
    	return -1;
    }
    //求出度
    public int outDegree(int v){
    	int outDegree=0;
    	Vertex<AnyType> ver=vertexs.get(v);
    	Arc arc=ver.firstArc;
    	while(arc!=null){
    		outDegree++;
    		arc=arc.nextArc;
    	}
    	return outDegree;
    }
    //求入度
    public int inDegree(int v){
    	int inDegree=0;
    	for(int i=0;i<vexNum;i++){
    		if(i==v)continue;
    		Vertex<AnyType> ver=vertexs.get(i);
    		Arc arc=ver.firstArc;
    		while(arc!=null){
    			if(arc.adjVex==v){
    				inDegree++;
    				break;
    			}
    			arc=arc.nextArc;
    		}
    	}
    	return inDegree;
    }
    //邻接表转换为邻接矩阵
    public int[][] getAdjacencyMatrix(){
    	int[][] adjacencyMatrix=new int[vexNum][vexNum];
    	for(int i=0;i<vexNum;i++){
    		Vertex<AnyType> ver=vertexs.get(i);
    		Arc arc=ver.firstArc;
    		for(int j=firstAdjVex(i);j>=0;j=nextAdjVex(i,j)){
    			adjacencyMatrix[i][j]=arc.weight;
    			arc=arc.nextArc;
    		}
    	}
    	return adjacencyMatrix;
    }
    //打印邻接矩阵
    public void printAdjacencyMatrix(){
    	int[][] adjacencyMatrix=getAdjacencyMatrix();
    	for(int i=0;i<adjacencyMatrix.length;i++)
    	{
    		for(int j=0;j<adjacencyMatrix[i].length;j++)
    			System.out.print(adjacencyMatrix[i][j]+" ");
    		System.out.println();
    	}
    }
    //插入节点
    public void insertVertex(AnyType data){
    	vertexs.add(new Vertex<AnyType>(data));
    	vexNum++;
    }
    //删除节点
    public void delete(AnyType a){
    	int i;
    	for(i=0;i<vexNum;i++)
    		if(vertexs.get(i).data.equals(a))
    			break;
    	if(i==vexNum){
    		System.out.println("您所删除的元素不存在!");
    		return;
    	}
    	for(int j=firstAdjVex(i);j>=0;j=nextAdjVex(i,j)){
    		if(type==2||type==4)
    			vertexs.get(j).degree--;
    		else{
    			vertexs.get(j).inDegree--;
    			vertexs.get(j).degree--;
    		}
    	}
    	vertexs.remove(i);
    	vexNum--;
    	for(int j=0;j<vexNum;j++){
    		Vertex<AnyType> v=vertexs.get(j);
    		Arc arc=v.firstArc;
    		if(arc!=null){
    			if(arc.adjVex==i){
    				v.firstArc=v.firstArc.nextArc;
    				if(type==2||type==4)
    					v.degree--;
    				else{
    					v.outDegree--;
    					v.degree--;
    				}
    				continue;
    			}
    		while(arc.nextArc!=null){
    			if(arc.nextArc.adjVex==i){
    				arc.nextArc=arc.nextArc.nextArc;
    				if(type==2||type==4){
    					v.degree--;
    				}
    				else if(type==1||type==3){
    					arc=arc.nextArc;
    					v.outDegree--;
    					v.degree--;
    				}
    				break;
    			}
    			arc=arc.nextArc;
    		}
    	}
    }
   }
    //删除边
    public void removeEdege(int i,int a){
    	Arc q=vertexs.get(i).firstArc;
    	if(q.adjVex==a)
    		vertexs.get(i).firstArc=q.nextArc;
    	while(q.nextArc!=null){
    		if(q.nextArc.adjVex==a){
    			q.nextArc=q.nextArc.nextArc;
    			break;
    		}
    		q=q.nextArc;
    	}
    }
    //增加边
    public void addEdege(int ss[],int ee[],int num){
    	for(int i=0;i<num;i++){
    		Arc tmp1=new Arc();
    		tmp1.adjVex=ee[i];
    		Arc q=vertexs.get(ss[i]).firstArc;
    		tmp1.nextArc=q;
    		vertexs.get(ss[i]).firstArc=tmp1;
    	}
    }
    //DFS
    void DFS(int start){
    	if(start>=vexNum)return;
    	Vertex<AnyType> tmp=vertexs.get(start);
    	tmp.vis=true;
    	System.out.print(tmp.data+" ");
    	if(tmp.firstArc!=null){
    		for(int w=tmp.firstArc.adjVex;w>=0;w=nextAdjVex(start,w)){
    			if(vertexs.get(w).vis==false)
    				DFS(w);
    		}
    	}
    }
    //BFS
    void BFS(int start){
    	Queue<Integer> queue=new LinkedList<Integer>();
    	vertexs.get(start).vis=true;
    	queue.add(start);
    	while(!queue.isEmpty()){
    		int tmp1=queue.remove();
    		System.out.print(vertexs.get(tmp1).data+" ");
    		int tmp2=firstAdjVex(tmp1);
    		while(tmp2!=-1){
    			if(vertexs.get(tmp2).vis==false){
    				vertexs.get(tmp2).vis=true;
    				queue.add(tmp2);
    			}
    			tmp2=nextAdjVex(tmp1,tmp2);
    		}
    	}
    }
    //深度优先遍历最小生成树
    public void DFSTree(int v){
    	first=new Node<AnyType>();
    	first.data=vertexs.get(v).data;
    	DFSTree(v,first);
    }
    void DFSTree(int v,Node T){
    	vertexs.get(v).vis=true;
    	int w;
    	Node p;
    	boolean first=true;
    	Node q=null;
    	System.out.print(T.data+" ");
    	for(w=firstAdjVex(v);w>=0;w=nextAdjVex(v,w))
    		if(!vertexs.get(w).vis){
    			p=new Node(vertexs.get(w).data);
    			if(first){
    				T.firstChild=p;
    				first=false;
    			}
    			else q.Sibling=p;
    			q=p;
    			DFSTree(w,q);
    		}
    }
    //广度优先遍历最小生成树(先序遍历)
    public void preOrderTree(){
    	if(first!=null)preOrderTree(first);
    }
    public void preOrderTree(Node<AnyType> t){
    	if(t!=null){
    		System.out.print(t.data+" ");//遍历根
    		preOrderTree(t.firstChild);//遍历左子树
    		preOrderTree(t.Sibling);//遍历右子树
    	}
    }
    //判断有向图是否存在环
    public boolean isDGCircle(){
    	Queue<Vertex> q=new LinkedList<Vertex>();
    	int count=0;
    	for(int i=0;i<vertexs.size();i++)
    		if(vertexs.get(i).inDegree==0)
    			q.add(vertexs.get(i));
    	while(!q.isEmpty()){
    		Vertex v=q.poll();
    		count++;
    		Arc p=null;
    		for(p=v.firstArc;p!=null;p=p.nextArc)
    			if(--vertexs.get(p.adjVex).inDegree==0)
    				q.add(vertexs.get(p.adjVex));
    	}
    	if(count!=vexNum)return false;
    	else return true;
    }
    //判断无向图是否存在环
    public boolean UDGCircle(){
    	if(arcNum>vexNum-1)
    		return false;
    	else return true;
    }
    //BFS判断是否存在路径
    public boolean BFSRoute(Graph<AnyType> g,int u,int v){
    	Queue<Integer> queue=new LinkedList<Integer>();
    	g.vertexs.get(u).vis=true;
    	queue.add(u);
    	while(!queue.isEmpty()){
    		int tmp1=queue.remove();
    		if(tmp1==v){
    			for(int i=0;i<g.vertexs.size();i++)
    				g.vertexs.get(i).vis=false;
    			return true;
    		}
    		int tmp2=firstAdjVex(tmp1);
    		while(tmp2!=-1){
    			if(g.vertexs.get(tmp2).vis==false){
    				queue.add(tmp2);
    				g.vertexs.get(tmp2).vis=true;
    			}
    			tmp2=nextAdjVex(tmp1,tmp2);
    		}
    	}
    	for(int i=0;i<g.vertexs.size();i++)
    		g.vertexs.get(i).vis=false;
    	return false;
    }
    //判断U到V是否存在路径
    public boolean isURouteV(Graph<AnyType> g,int u,int v){
    	if(BFSRoute(g,u,v)==true)
    		return true;
    	return false;
    }
    //求一条从U到V的简单路径
    public LinkedList<Integer> oneURouteV(Graph<AnyType> g,int u,int v){
    	Map<Integer,Integer> parent=new HashMap<Integer,Integer>();
    	LinkedList<Integer> tmp=new LinkedList<Integer>();
    	if(isURouteV(g,u,v)==true){
    		Queue<Integer> queue=new LinkedList<Integer>();
    		for(int i=0;i<g.vertexs.size();i++)
    			parent.put(i,i);
    		g.vertexs.get(u).vis=true;
    		queue.add(u);
    		while(!queue.isEmpty()){
    			int tmp1=queue.remove();
    			if(tmp1==v)break;
    			int tmp2=firstAdjVex(tmp1);
    			while(tmp2!=-1){
    				if(g.vertexs.get(tmp2).vis==false){
    					parent.put(tmp2,tmp1);
    					queue.add(tmp2);
    					g.vertexs.get(tmp2).vis=true;
    				}
    				tmp2=nextAdjVex(tmp1,tmp2);
    			}
    		}
    		//存路径
    		int x=v;
    		while(x!=u){
    			tmp.add(x);
    			x=parent.get(x);
    		}
    		tmp.add(u);
    		return tmp;
    	}
    	else{
    		System.out.println("两点之间没有路径。");
    		return null;
    	}
    }
    //求U到V的所有路径
    public void allURouteV(Graph<AnyType> g,int u,int v){
    	LinkedList<Integer> stack=new LinkedList<Integer>();
    	count=1;
    	stack.addLast(u);
    	for(int i=firstAdjVex(u);i!=-1;i=nextAdjVex(u,i)){
    		if(g.vertexs.get(i).vis==false){
    			g.vertexs.get(i).vis=true;
    			stack.addLast(i);
    			search(g,i,v,stack);
    			g.vertexs.get(i).vis=false;
    			stack.pollLast();
    		}
    	}
    }
    public void search(Graph<AnyType> g,int x,int v,LinkedList<Integer> ss){
    	if(x==v){
    		System.out.print("路径"+count+":");
    		count++;
    		for(int i=0;i<ss.size()-1;i++)
    			System.out.print(g.vertexs.get(ss.get(i)).data+"->");
    		System.out.println(g.vertexs.get(ss.get(ss.size()-1)).data);
    		return;
    	}
    	else{
    		for(int j=firstAdjVex(x);j!=-1;j=nextAdjVex(x,j))
    			if(g.vertexs.get(j).vis==false){
    				g.vertexs.get(j).vis=true;
    				ss.addLast(j);
    				search(g,j,v,ss);
    				g.vertexs.get(j).vis=false;
    				ss.pollLast();
    			}
    	}
    }
    //Dijkstra
    int pre[]=new int[107];
    Map<Integer,Integer> parents=new HashMap<Integer,Integer>();
    public void Dijkstra(Graph<AnyType> g,Vertex<AnyType> s,int start){
    	LinkedList<Integer> tmp=new LinkedList<Integer>();
    	for(int i=0;i<g.vertexs.size();i++){
    		pre[i]=start;
    		g.vertexs.get(i).dist=INF;
    		g.vertexs.get(i).vis=false;
    		parents.put(i,i);
    	}
    	s.dist=0;
    	for(int i=0;i<g.vertexs.size();i++){
    		int min=INF;
    		int v=0;
    		for(int j=0;j<g.vertexs.size();j++)
    			if(g.vertexs.get(j).vis==false&&g.vertexs.get(j).dist<min){
    				min=g.vertexs.get(j).dist;
    				v=j;
    			}
    		g.vertexs.get(v).vis=true;
    		for(int j=0;j<g.vertexs.size();j++)
    			if(g.vertexs.get(j).vis==false&&weigthWith(v,j)!=-1){
    				if(g.vertexs.get(j).dist>g.vertexs.get(v).dist+weigthWith(v,j)){
    					g.vertexs.get(j).dist=g.vertexs.get(v).dist+weigthWith(v,j);
    					pre[j]=v;
    					parents.put(j,v);
    				}
    			}
    	}
    }
    public void printPath(Graph<AnyType>g,int u,int v){
    	int x=v;
    	LinkedList<Integer> tmp=new LinkedList<Integer>();
    	while(x!=u){
    		tmp.add(x);
    		x=parents.get(x);
    	}
    	tmp.add(u);
    	for(int i=tmp.size()-1;i>0;i--)
			System.out.print(g.vertexs.get(tmp.get(i)).data+"->");
    	System.out.println(g.vertexs.get(tmp.get(0)).data);
    }
    public void printPath(int s,int e,int num){
    	int a[]=new int[107],i,k;
    	k=0;
    	a[k++]=e;
    	i=e;
    	while(s!=i){
    		i=pre[i];
    		a[k++]=i;
    	}
    	System.out.print("Path"+num+": ");
    	for(i=k-1;i>0;i--)
    		System.out.print(a[i]+"-->");
    	System.out.println(a[0]);
    }
    //判断v到w是否存在路径,并返回其权值
    public int weigthWith(int v,int w){
    	Arc p=vertexs.get(v).firstArc;
    	if(p==null)return -1;
    	while(p!=null&&p.adjVex!=w)
    		p=p.nextArc;
    	if(p==null)return -1;
    	return p.weight;
    }
    //Kruskal
    public int Kruskal(Graph<AnyType> g){
    	int edgesAccept=0,ans=0;
    	LinkedList<Arc> minTree=new LinkedList<Arc>();
    	PriorityQueue<Arc> pq=new PriorityQueue<Arc>(100,new Comparator<Arc>(){
    		public int compare(Arc a,Arc b){
    			if(a.weight>b.weight)return 1;
    			else return -1;
    		}
    	});
    	for(int i=0;i<g.vertexs.size();i++){
    		Arc p=g.vertexs.get(i).firstArc;
    		while(p!=null){
    			pq.add(p);
    			p=p.nextArc;
    		}
    	}
    	DisjSets ds=new DisjSets(100);
    	Arc edge;
    	int u,v;
    	while(edgesAccept<g.vertexs.size()-1){
    		edge=pq.poll();
    		u=edge.vex;
    		v=edge.adjVex;
    		int uset=ds.find(u);
    		int vset=ds.find(v);
    		if(uset!=vset){
    			edgesAccept++;
    			ds.union(u,v);
    			minTree.add(edge);
    			ans+=edge.weight;
    		}
    	}
    	return ans;
    }
    //prime
    public void prime(Graph<AnyType> g){
    	for(int i=0;i<g.vertexs.size();i++){
    		g.vertexs.get(i).primDist=INF;
    		g.vertexs.get(i).primVis=false;
    	}
    	g.vertexs.get(0).primDist=0;
    	int count=0;
    	for(int i=0;i<g.vertexs.size();i++){
    		int min=INF;
    		int v=0;
    		for(int j=0;j<g.vertexs.size();j++)
    			if(g.vertexs.get(j).primVis==false&&g.vertexs.get(i).primDist<min){
    				min=g.vertexs.get(i).primDist;
    				v=j;
    			}
    		g.vertexs.get(v).primVis=true;
    		count+=min;
    		System.out.print(g.vertexs.get(v).data+" ");
    		if(i==g.vertexs.size()-1)
    		{
    			System.out.println("\n最小生成树的值是:"+count);
    			break;
    		}
    		for(int j=0;j<g.vertexs.size();j++)
    			if(g.vertexs.get(j).primVis==false&&weigthWith(v,j)!=-1)
    				if(g.vertexs.get(j).primDist>weigthWith(v,j))
    					g.vertexs.get(j).primDist=weigthWith(v,j);
    	}
    }
    	public void Floyd(){
    		int[][] adjacencyMartix=getAdjacencyMatrix();
    		for(int i=0;i<adjacencyMartix.length;i++)
    			for(int j=0;j<adjacencyMartix[i].length;j++)
    				if(adjacencyMartix[i][j]==0)
    					adjacencyMartix[i][j]=INF;
    		for(int k=0;k<adjacencyMartix.length;k++)
    			for(int i=0;i<adjacencyMartix.length;i++)
    				for(int j=0;j<adjacencyMartix[i].length;j++)
    					if(i!=j&&adjacencyMartix[i][k]+adjacencyMartix[k][j]<adjacencyMartix[i][j])
    						adjacencyMartix[i][j]=adjacencyMartix[i][k]+adjacencyMartix[k][j];
    		for(int i=0;i<adjacencyMartix.length;i++){
    			System.out.print("点"+i+"到其它点的最短距离是:");
    			for(int j=0;j<adjacencyMartix[i].length;j++)
    				if(adjacencyMartix[i][j]==INF)
    						System.out.print("不存在  ");
    				else System.out.print(adjacencyMartix[i][j]+"  ");
    			System.out.println();
    		}
    }
}

package GraphDemo;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test{
	public static void main(String args[]){
		Scanner cin=new Scanner(System.in);
		int INF=0xfffff;
		//建图
		System.out.println("请输入要建的图的类型:\n1:有向图  2:无向图  3:有向网  4:无向网");
		int type=cin.nextInt();
		System.out.println("请输入节点个数和边的条数:");
		int vnum=cin.nextInt();
		int anum=cin.nextInt();
		Graph<String>graph =new Graph<String>(type,vnum,anum);
		List<String>v=new ArrayList<String>();
		List<Integer> a=new ArrayList<Integer>();
		List<Integer> b=new ArrayList<Integer>();
		List<Integer> x=new ArrayList<Integer>();
		System.out.println("请输入"+vnum+"个节点:");
		for(int i=0;i<vnum;i++)
			v.add(cin.next());
		if(type==3||type==4)
			System.out.println("请输入"+anum+"条边及其权值:");
		else System.out.println("请输入"+anum+"条边:");
		for(int i=0;i<anum;i++){
			a.add(cin.nextInt());
			b.add(cin.nextInt());
			if(type==3||type==4)
				x.add(cin.nextInt());
			else x.add(1);
		}
		graph.createGraph(type,v,a,b,x);
		//邻接表转换为邻接矩阵
		System.out.println("邻接表转换为邻接矩阵是:");
		graph.printAdjacencyMatrix();
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//增加顶点
		/*System.out.println("请输入要增加顶点的个数:");
		int addnum=cin.nextInt();
		System.out.println("请输入顶点:");
		for(int i=0;i<addnum;i++)
			graph.insertVertex(cin.next());
		//增加边
		System.out.println("请输入要增加边的条数:");
		int addarc=cin.nextInt();
		int ss[]=new int [100];
		int ee[]=new int [100];
		System.out.println("请输入边的信息:");
		for(int i=0;i<addarc;i++){
			ss[i]=cin.nextInt();
			ee[i]=cin.nextInt();
		}
		graph.addEdege(ss,ee,addarc);
		//删除点
		System.out.println("请输入要删除的点的个数:");
		int delenum=cin.nextInt();
		System.out.println("请输入要删除点的信息:");
		for(int i=0;i<delenum;i++)
			graph.delete(cin.next());
		//删除边
		System.out.println("请输入要删除边的条数:");
		int delearc=cin.nextInt();
		System.out.println("请输入要删除边的信息:");
		for(int i=0;i<delearc;i++)
			graph.removeEdege(cin.nextInt(),cin.nextInt());
		//DFS
		int count=0;
		System.out.println("图的深度优先遍历结果是:");
		for(int i=0;i<graph.vertexs.size();i++)
			if(graph.vertexs.get(i).vis==false){
				graph.DFS(i);
				count++;
			}
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		System.out.println();
		//BFS
		System.out.println("图的广度优先遍历结果是:");
		for(int i=0;i<graph.vertexs.size();i++)
			if(graph.vertexs.get(i).vis==false){
				graph.BFS(i);
			}
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		System.out.println();
		System.out.println("DFS生成树和BFS生成树,请输入要遍历的点的编号:");
		//DFS生成树
		int startnum=cin.nextInt();
		System.out.println("图的深度优先生成树:");
		graph.DFSTree(startnum);
		System.out.println();
		//BFS生成树
		System.out.println("图的广度优先生成树:");
		graph.preOrderTree();
		System.out.println();
		//判断联通分量的个数
		System.out.println("图的连通分量的个数是:"+count);
		//判断是否存在环
		//判断有向图是否存在环
		if(type==1){
			for(int i=0;i<graph.vertexs.size();i++){
				graph.vertexs.get(i).inDegree=graph.inDegree(i);
				graph.vertexs.get(i).outDegree=graph.outDegree(i);
			}
			if(graph.isDGCircle())
				System.out.println("不存在环。");
			else
				System.out.println("存在环");
		}
		//判断无向图是否存在环
		else if(type==2){
			if(graph.UDGCircle())
				System.out.println("不存在环。");
			else
				System.out.println("存在环");
		}
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//判断u到v是否存在路径
		System.out.println("请输入u和v判断,u->v是否存在路径");
		if(graph.isURouteV(graph,cin.nextInt(),cin.nextInt()))
			System.out.println("u到v存在路径。");
		else System.out.println("u到v不存在路径。");
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//求一条u到v的路径
		System.out.println("请输入u和v并求出u->v的一条简单路径");
		LinkedList<Integer> g=graph.oneURouteV(graph,cin.nextInt(),cin.nextInt());
		for(int i=g.size()-1;i>=0;i--)
			System.out.print(graph.vertexs.get(g.get(i)).data+" ");
		System.out.println();
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//求u到v的所有路径
		System.out.println("请输入u和v并求出u->v的所有路径");
		graph.allURouteV(graph,cin.nextInt(),cin.nextInt());
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;*/
		//Dijkstra
		System.out.println("Dijkstra算法\n请输入起点:");
		int startDij=cin.nextInt(),num=0;
		graph.Dijkstra(graph,graph.vertexs.get(startDij),startDij);
		for(int i=0;i<graph.vertexs.size();i++)
			if(graph.vertexs.get(i).dist!=INF&&i!=startDij){
				graph.printPath(graph,startDij,i);
				graph.printPath(startDij,i,++num);
			}
				
		System.out.println("点"+startDij+"到其它点的最短路径分别是:");
		for(int i=0;i<graph.vertexs.size();i++)
			if(graph.vertexs.get(i).dist!=INF)
				System.out.println("到点"+i+"的最短距离是:"+graph.vertexs.get(i).dist);
			else System.out.println("到点"+i+"不存在最短距离。");
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//Floyd
		System.out.println("Floyd算法");
		graph.Floyd();
		//Prime
		System.out.println("Prime算法");
		graph.prime(graph);
		System.out.println();
		for(int i=0;i<graph.vertexs.size();i++)
			graph.vertexs.get(i).vis=false;
		//Kruskal
		System.out.println("Kruskal算法");
		int ans=graph.Kruskal(graph);
		System.out.println("Kruskal求得的最小生成树结果是: "+ans);
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值