数据结构算法系列2-图

图是由顶点(vertex)和边(edge)组成的一种结构。顶点的集合V,边的集合是E,所以图记为G = (V,E)。
数据结构算法系列2-图_第1张图片
有向图是边有方向,无向图是边之间没有方向。无向图顶点的边数叫度,有向图顶点的边数叫出度和入度
。连通图是图的任意两个顶点都有路径可达。如果图的边有值,成为权值。图一般在程序中用矩阵,也就是数组表示。

1.图的深度优先遍历

访问顺序是: A -> C -> B -> D -> F -> G -> E
数据结构算法系列2-图_第2张图片

public class GraphList {
	 //节点数目
    protected int size;
    //定义数组,保存顶点信息
    protected String[] nodes;

    //定义矩阵保存顶点信息
    protected int[][] edges;
    private int[] visit;
    public GraphList() {
		super();
		 //初始化顶点
        nodes = new String[]{"A","B","C","D","E","F","G"};
        size=nodes.length;

        //初始化边---- 为了直观,做一个常量定义
        final int A=0,B=1,C=2,D=3,E=4,F=5,G=6;
        edges = new int[size][size];
        edges[A][C] = 1;
        edges[A][D] = 1;
        edges[A][F] = 1;
        edges[B][C] = 1;
        edges[C][A] = 1;
        edges[C][D] = 1;
        edges[C][B] = 1;
        edges[D][A] = 1;
        edges[D][C] = 1;
        edges[E][G] = 1;
        edges[F][A] = 1;
        edges[F][G] = 1;
        edges[G][F] = 1;
        edges[G][E] = 1;
        visit = new int[size];     //遍历标志,防止死环遍历
	}

   public  void deepList(int index){
	   System.out.print(nodes[index]+"->");
	   visit[index]=1;
	   for (int i = 0; i < size; i++) {
		if (edges[index][i]==1&&visit[i]!=1) {
			  deepList(i);
		}
	}
	   
   }

    public static void main(String[] args) {
        GraphList graph = new GraphList();
        graph.deepList(0);
    }
}

2.图的广度优先遍历

访问顺序是: A -> C -> D -> F -> B -> G -> E
数据结构算法系列2-图_第3张图片

public class GraphList {
	 //节点数目
    protected int size;
    //定义数组,保存顶点信息
    protected String[] nodes;

    //定义矩阵保存顶点信息
    protected int[][] edges;
    private int[] visit;
    private int[] quene;

    public GraphList() {
		super();
		 //初始化顶点
        nodes = new String[]{"A","B","C","D","E","F","G"};
        size=nodes.length;

        //初始化边---- 为了直观,做一个常量定义
        final int A=0,B=1,C=2,D=3,E=4,F=5,G=6;
        edges = new int[size][size];
        edges[A][C] = 1;
        edges[A][D] = 1;
        edges[A][F] = 1;
        edges[B][C] = 1;
        edges[C][A] = 1;
        edges[C][D] = 1;
        edges[C][B] = 1;
        edges[D][A] = 1;
        edges[D][C] = 1;
        edges[E][G] = 1;
        edges[F][A] = 1;
        edges[F][G] = 1;
        edges[G][F] = 1;
        edges[G][E] = 1;
        visit = new int[size];     //遍历标志,防止死环遍历
        quene = new int[size]; 
	}

   public  void BreadthList(int start,int end){
	   int last = end;
	   for (int i = start; i <=end; i++) {
			System.out.print(nodes[quene[i]]+"->");
		   for (int j = 0; j < size; j++) {
				if (edges[quene[i]][j]==1&&visit[j]!=1) {
					visit[j]=1;
					quene[++last]=j;
				}
			}
	}
	   
	 if (end

3.图的最短路径算法(Dijkstra)

1、扫描AA邻接点,记录邻接点权重值
2、找出邻接点里最小的那个值
数据结构算法系列2-图_第4张图片

public class Dijkstra {
	//节点数目
	protected int size;
	//定义数组,保存顶点信息
	protected String[] nodes;

	//定义矩阵保存顶点信息
	protected int[][] edges;

	private int[] isMarked;//节点确认--中心标识
	private String[] path;//源到节点的路径信息
	private int[] distances;//源到节点的距离

	public Dijkstra2(){
		init();

		isMarked = new int[size];
		path = new String[size];
		distances = new int[size];

		for (int i=0;i 0){
				//计算AA节点到 i节点的权重值
				int distant = distances[node] + this.edges[node][i];
				if (distant < distances[i]){
					distances[i] = distant;
					path[i] = path[node] +"-->"+ nodes[i];
				}
			}
		}
	}

//	2、找出邻接点里最小的那个值
	private int getShort(){
		int last = -1;

		int min = Integer.MAX_VALUE;
		for (int i=0;i

你可能感兴趣的:(数据结构和算法)