Java数据结构——带权图
带权图的最小生成树——Prim算法和Kruskal算法
带权图的最短路径算法——Dijkstra算法
- package graph;
- // path.java
- // demonstrates shortest path with weighted, directed graphs 带权图的最短路径算法
- // to run this program: C>java PathApp
- ////////////////////////////////////////////////////////////////
- class DistPar // distance and parent
- { // items stored in sPath array
- public int distance; // distance from start to this vertex
- public int parentVert; // current parent of this vertex
- // -------------------------------------------------------------
- public DistPar(int pv, int d) // constructor
- {
- distance = d;
- parentVert = pv;
- }
- // -------------------------------------------------------------
- } // end class DistPar
- ///////////////////////////////////////////////////////////////
- class Vertex
- {
- public char label; // label (e.g. 'A')
- public boolean isInTree;
- // -------------------------------------------------------------
- public Vertex(char lab) // constructor
- {
- label = lab;
- isInTree = false;
- }
- // -------------------------------------------------------------
- } // end class Vertex
- ////////////////////////////////////////////////////////////////
- class Graph
- {
- private final int MAX_VERTS = 20;
- private final int INFINITY = 1000000;
- private Vertex vertexList[]; // list of vertices
- private int adjMat[][]; // adjacency matrix
- private int nVerts; // current number of vertices
- private int nTree; // number of verts in tree
- private DistPar sPath[]; // array for shortest-path data
- private int currentVert; // current vertex
- private int startToCurrent; // distance to currentVert
- // -------------------------------------------------------------
- public Graph() // constructor
- {
- vertexList = new Vertex[MAX_VERTS];
- // adjacency matrix
- adjMat = new int[MAX_VERTS][MAX_VERTS];
- nVerts = 0;
- nTree = 0;
- for(int j=0; j<MAX_VERTS; j++) // set adjacency
- for(int k=0; k<MAX_VERTS; k++) // matrix
- adjMat[j][k] = INFINITY; // to infinity
- sPath = new DistPar[MAX_VERTS]; // shortest paths
- } // end constructor
- // -------------------------------------------------------------
- public void addVertex(char lab)
- {
- vertexList[nVerts++] = new Vertex(lab);
- }
- // -------------------------------------------------------------
- public void addEdge(int start, int end, int weight)
- {
- adjMat[start][end] = weight; // (directed)
- }
- // -------------------------------------------------------------
- public void path() // find all shortest paths
- {
- int startTree = 0; // start at vertex 0
- vertexList[startTree].isInTree = true;
- nTree = 1; // put it in tree
- // transfer row of distances from adjMat to sPath
- for(int j=0; j<nVerts; j++)
- {
- int tempDist = adjMat[startTree][j];
- sPath[j] = new DistPar(startTree, tempDist);
- }
- // until all vertices are in the tree
- while(nTree < nVerts)
- {
- int indexMin = getMin(); // get minimum from sPath
- int minDist = sPath[indexMin].distance;
- if(minDist == INFINITY) // if all infinite
- { // or in tree,
- System.out.println("There are unreachable vertices");
- break; // sPath is complete
- }
- else
- { // reset currentVert
- currentVert = indexMin; // to closest vert
- startToCurrent = sPath[indexMin].distance;
- // minimum distance from startTree is
- // to currentVert, and is startToCurrent
- }
- // put current vertex in tree
- vertexList[currentVert].isInTree = true;
- nTree++;
- adjust_sPath(); // update sPath[] array
- } // end while(nTree<nVerts)
- displayPaths(); // display sPath[] contents
- nTree = 0; // clear tree
- for(int j=0; j<nVerts; j++)
- vertexList[j].isInTree = false;
- } // end path()
- // -------------------------------------------------------------
- public int getMin() // get entry from sPath
- { // with minimum distance
- int minDist = INFINITY; // assume minimum
- int indexMin = 0;
- for(int j=1; j<nVerts; j++) // for each vertex,
- { // if it's in tree and
- if( !vertexList[j].isInTree && // smaller than old one
- sPath[j].distance < minDist )
- {
- minDist = sPath[j].distance;
- indexMin = j; // update minimum
- }
- } // end for
- return indexMin; // return index of minimum
- } // end getMin()
- // -------------------------------------------------------------
- public void adjust_sPath()
- {
- // adjust values in shortest-path array sPath
- int column = 1; // skip starting vertex
- while(column < nVerts) // go across columns
- {
- // if this column's vertex already in tree, skip it
- if( vertexList[column].isInTree )
- {
- column++;
- continue;
- }
- // calculate distance for one sPath entry
- // get edge from currentVert to column
- int currentToFringe = adjMat[currentVert][column];
- // add distance from start
- int startToFringe = startToCurrent + currentToFringe;
- // get distance of current sPath entry
- int sPathDist = sPath[column].distance;
- // compare distance from start with sPath entry
- if(startToFringe < sPathDist) // if shorter,
- { // update sPath
- sPath[column].parentVert = currentVert;
- sPath[column].distance = startToFringe;
- }
- column++;
- } // end while(column < nVerts)
- } // end adjust_sPath()
- // -------------------------------------------------------------
- public void displayPaths()
- {
- for(int j=0; j<nVerts; j++) // display contents of sPath[]
- {
- System.out.print(vertexList[j].label + "="); // B=
- if(sPath[j].distance == INFINITY)
- System.out.print("inf"); // inf
- else
- System.out.print(sPath[j].distance); // 50
- char parent = vertexList[ sPath[j].parentVert ].label;
- System.out.print("(" + parent + ") "); // (A)
- }
- System.out.println("");
- }
- // -------------------------------------------------------------
- } // end class Graph
- ////////////////////////////////////////////////////////////////
- class path
- {
- public static void main(String[] args)
- {
- Graph theGraph = new Graph();
- theGraph.addVertex('A'); // 0 (start)
- theGraph.addVertex('B'); // 1
- theGraph.addVertex('C'); // 2
- theGraph.addVertex('D'); // 3
- theGraph.addVertex('E'); // 4
- theGraph.addEdge(0, 1, 50); // AB 50
- theGraph.addEdge(0, 3, 80); // AD 80
- theGraph.addEdge(1, 2, 60); // BC 60
- theGraph.addEdge(1, 3, 90); // BD 90
- theGraph.addEdge(2, 4, 40); // CE 40
- theGraph.addEdge(3, 2, 20); // DC 20
- theGraph.addEdge(3, 4, 70); // DE 70
- theGraph.addEdge(4, 1, 50); // EB 50
- System.out.println("Shortest paths");
- theGraph.path(); // shortest paths
- System.out.println();
- } // end main()
- } // end class PathApp
- ////////////////////////////////////////////////////////////////
Java数据结构——带权图的更多相关文章
- java数据结构----带权图
1.带权图:要引入带权图,首先要引入最小生成树,当所有的边拥有相同的权值时.问题变得简单了,算法可以选择任意一条边加入最小生成树.但是当边有不同的权值时,需要用一些算法决策来选择正确的边. 2.带权图 ...
- 带权图的最短路径算法(Dijkstra)实现
一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...
- 无向带权图的最小生成树算法——Prim及Kruskal算法思路
边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...
- C语言——无向带权图邻接矩阵的建立
#include <stdio.h> #include "Graph.h" #define MAX_INT 32767 /* #define vnum 20 #defi ...
- (5)Java数据结构--有继承图,用途分析
java 中几种常用数据结构 - u010947402的博客 - CSDN博客http://blog.csdn.net/u010947402/article/details/51878166 JAVA ...
- java数据结构_笔记(4)_图
图一.概念.图: 是一种复杂的非线性数据结构.图的二元组定义: 图 G 由两个集合 V 和 E 组成,记为:G=(V, E) 其中: V 是顶点的有穷非空集合,E 是 V 中顶点偶对(称为边)的有穷 ...
- java数据结构----图
1.图:.在计算机程序设计中,图是最常用的数据结构之一.对于存储一般的数据问题,一般用不到图.但对于某些(特别是一些有趣的问题),图是必不可少的.图是一种与树有些相像的数据结构,从数学意义上来讲,树是 ...
- java数据结构_笔记(5)_图的算法
图的算法 1 图的遍历图的遍历就是从图中某个顶点出发,按某种方法对图中所有顶点访问且仅访问一次.遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础. 2 深度优先遍历从图中某个顶点V 出发 ...
- Java数据结构和算法(十五)——无权无向图
前面我们介绍了树这种数据结构,树是由n(n>0)个有限节点通过连接它们的边组成一个具有层次关系的集合,把它叫做“树”是因为它看起来像一棵倒挂的树,包括二叉树.红黑树.2-3-4树.堆等各种不同的 ...
随机推荐
- Android入门笔记(一)
第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安 ...
- Ubuntu配置Ruby和Rails
安装curl sudo apt-get install curl 安装RVM curl -L https://get.rvm.io | bash -s stable 通过RVM来安装Ruby rvm ...
- Linux 下系统调用的三种方法
系统调用(System Call)是操作系统为在用户态运行的进程与硬件设备(如CPU.磁盘.打印机等)进行交互提供的一组接口.当用户进程需要发生系统调用时,CPU 通过软中断切换到内核态开始执行内核系 ...
- 天河2号荣膺第41届TOP500榜首
国际TOP500组织在6月17日公布最新全球超级计算机500强榜单,由中国国防科技大学研制的“天河二号”以每秒33.86千万亿次的浮点运算速度成为全球最快的超级计算机. 天河2号(又称银河2号),将在 ...
- uva 140 bandwidth (好题) ——yhx
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...
- bzoj-4514(网络流)
题目链接: 4514: [Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数 ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- python文件调用
如果列表T是a.py中是全局的,则直接调用即可,例如 #a.py T = [1,2,3,4] #b.py import a def test(): for i in a.T: ...
- perl 调用shell脚本
perl调用shell命令 perl调用shell shell调用perl Perl执行shell命令的几种方式及其区别
- FineUI小技巧(7)多表头表格导出
前言 之前我们曾写过一篇文章 FineUI小技巧(3)表格导出与文件下载,对于在 FineUI 中导出表格数据进行了详细描述.今天我们要更进一步,介绍下如何导出多表头表格. 多表头表格的标签定义 在 ...