GraphUtil

import java.util.LinkedList;public class GraphUtil {//深度优先遍历public static void dfs(Graph graph, int start, boolean[] visited) {System.out.println(graph.vertexes[start].data);visited[start] = true;for (int index : graph.adj[start]) {if (!visited[index]) {dfs(graph, index, visited);}}}//广度优先遍历public static void bfs(Graph graph, int start, boolean[] visited, LinkedList<Integer> queue) {queue.offer(start);while (!queue.isEmpty()) {int front = queue.poll();if (visited[front]) {continue;}System.out.println(graph.vertexes[front].data);visited[front] = true;for (int index : graph.adj[front]) {queue.offer(index);;}}}//图的顶点private static class Vertex {int data;Vertex(int data) {this.data = data;}}//图(邻接表形式)private static class Graph {private int size;private Vertex[] vertexes;private LinkedList<Integer>[] adj;Graph(int size) {this.size = size;//初始化顶点和邻接矩阵vertexes = new Vertex[size];adj = new LinkedList[size];for (int i = 0; i < size; i++) {vertexes[i] = new Vertex(i);adj[i] = new LinkedList();}}}public static void main(String[] args) {Graph graph = new Graph(6);graph.adj[0].add(1);graph.adj[0].add(2);graph.adj[0].add(3);graph.adj[1].add(0);graph.adj[1].add(3);graph.adj[1].add(4);graph.adj[2].add(0);graph.adj[3].add(0);graph.adj[3].add(1);graph.adj[3].add(4);graph.adj[3].add(5);graph.adj[4].add(1);graph.adj[4].add(3);graph.adj[4].add(5);graph.adj[5].add(3);graph.adj[5].add(4);System.out.println("图的深度优先遍历:");dfs(graph, 0, new boolean[graph.size]);System.out.println("图的广度优先遍历:");bfs(graph, 0, new boolean[graph.size], new LinkedList<Integer>());}
}

单源最短路径算法 Dijkstra 算法

import java.util.*;public class Dijkstra {// Dijkstra最短路径算法public static int[] dijkstra(Graph graph, int startIndex) {//图的顶点数量int size = graph.vertexes.length;//创建距离表,存储从起点到每一个顶点的临时距离int[] distances = new int[size];//记录顶点遍历状态boolean[] access = new boolean[size];//初始化最短路径表,到达每个顶点的路径代价默认为无穷大for (int i = 1; i < size; i++) {distances[i] = Integer.MAX_VALUE;}//遍历起点,刷新距离表access[0] = true;List<Edge> edgesFromStart = graph.adj[startIndex];for (Edge edge : edgesFromStart) {distances[edge.index] = edge.weight;}//主循环,重复遍历最短距离顶点和刷新距离表的操作for (int i = 1; i < size; i++) {//寻找最短距离顶点int minDistanceFromStart = Integer.MAX_VALUE;int minDistanceIndex = -1;for (int j = 1; j < size; j++) {if (!access[j] && (distances[j] < minDistanceFromStart)) {minDistanceFromStart = distances[j];minDistanceIndex = j;}}if (minDistanceIndex == -1) {break;}//遍历顶点,刷新距离表access[minDistanceIndex] = true;for (Edge edge : graph.adj[minDistanceIndex]) {if (access[edge.index]) {continue;}int weight = edge.weight;int preDistance = distances[edge.index];if ((weight != Integer.MAX_VALUE) &&((minDistanceFromStart + weight) < preDistance)) {distances[edge.index] = minDistanceFromStart + weight;}}}return distances;}// Dijkstra最短路径算法(返回完整路径)public static int[] dijkstraV2(Graph graph, int startIndex) {//图的顶点数量int size = graph.vertexes.length;//创建距离表,存储从起点到每一个顶点的临时距离int[] distances = new int[size];//创建前置定点表,存储从起点到每一个顶点的已知最短路径的前置节点int[] prevs = new int[size];//记录顶点遍历状态boolean[] access = new boolean[size];//初始化最短路径表,到达每个顶点的路径代价默认为无穷大for (int i = 0; i < size; i++) {distances[i] = Integer.MAX_VALUE;}//遍历起点,刷新距离表access[0] = true;List<Edge> edgesFromStart = graph.adj[startIndex];for (Edge edge : edgesFromStart) {distances[edge.index] = edge.weight;prevs[edge.index] = 0;}//主循环,重复 遍历最短距离顶点和刷新距离表 的操作for (int i = 1; i < size; i++) {//寻找最短距离顶点int minDistanceFromStart = Integer.MAX_VALUE;int minDistanceIndex = -1;for (int j = 1; j < size; j++) {if (!access[j] && (distances[j] < minDistanceFromStart)) {minDistanceFromStart = distances[j];minDistanceIndex = j;}}if (minDistanceIndex == -1) {break;}//遍历顶点,刷新距离表access[minDistanceIndex] = true;for (Edge edge : graph.adj[minDistanceIndex]) {if (access[edge.index]) {continue;}int weight = edge.weight;int preDistance = distances[edge.index];if ((weight != Integer.MAX_VALUE) &&((minDistanceFromStart + weight) < preDistance)) {distances[edge.index] = minDistanceFromStart + weight;prevs[edge.index] = minDistanceIndex;}}}return prevs;}private static void printPrevs(Vertex[] vertexes, int[] prev, int i) {if (i > 0) {printPrevs(vertexes, prev, prev[i]);}System.out.println(vertexes[i].data);}private static void initGraph(Graph graph) {graph.vertexes[0] = new Vertex("A");graph.vertexes[1] = new Vertex("B");graph.vertexes[2] = new Vertex("C");graph.vertexes[3] = new Vertex("D");graph.vertexes[4] = new Vertex("E");graph.vertexes[5] = new Vertex("F");graph.vertexes[6] = new Vertex("G");graph.adj[0].add(new Edge(1, 5));graph.adj[0].add(new Edge(2, 2));graph.adj[1].add(new Edge(0, 5));graph.adj[1].add(new Edge(3, 1));graph.adj[1].add(new Edge(4, 6));graph.adj[2].add(new Edge(0, 2));graph.adj[2].add(new Edge(3, 6));graph.adj[2].add(new Edge(5, 8));graph.adj[3].add(new Edge(1, 1));graph.adj[3].add(new Edge(2, 6));graph.adj[3].add(new Edge(4, 1));graph.adj[3].add(new Edge(5, 2));graph.adj[4].add(new Edge(1, 6));graph.adj[4].add(new Edge(3, 1));graph.adj[4].add(new Edge(6, 7));graph.adj[5].add(new Edge(2, 8));graph.adj[5].add(new Edge(3, 2));graph.adj[5].add(new Edge(6, 3));graph.adj[6].add(new Edge(4, 7));graph.adj[6].add(new Edge(5, 3));}//图的顶点private static class Vertex {String data;Vertex(String data) {this.data = data;}}//图的边private static class Edge {int index;int weight;Edge(int index, int weight) {this.index = index;this.weight = weight;}}// 图private static class Graph {private Vertex[] vertexes;private LinkedList<Edge>[] adj;Graph(int size) {//初始化顶点和邻接矩阵vertexes = new Vertex[size];adj = new LinkedList[size];for (int i = 0; i < adj.length; i++) {adj[i] = new LinkedList<Edge>();}}}public static void main(String[] args) {Graph graph = new Graph(7);initGraph(graph);int[] distances = dijkstra(graph, 0);System.out.println(distances[6]);System.out.println("输出完整路径:");int[] prevs = dijkstraV2(graph, 0);printPrevs(graph.vertexes, prevs, graph.vertexes.length- 1);}}

多源最短路径算法 Floyd 算法

public class Floyd {final static int INF = Integer.MAX_VALUE;public static void floyd(int[][] matrix) {//循环更新矩阵的值for (int k = 0; k < matrix.length; k++) {for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix.length; j++) {if ((matrix[i][k] == INF) || (matrix[k][j] == INF)) {continue;}matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]);}}}// 打印floyd最短路径的结果System.out.printf("最短路径矩阵: \n");for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix.length; j++) {System.out.printf("%3d  ", matrix[i][j]);}System.out.printf("\n");}}public static void main(String[] args) {int[][] matrix = {{ 0, 5, 2, INF, INF, INF, INF },{ 5, 0, INF, 1, 6, INF, INF },{ 2, INF, 0, 6, INF, 8, INF },{ INF, 1, 6, 0, 1, 2, INF },{ INF, 6, INF, 1, 0, INF, 7 },{ INF, INF, 8, 2, INF, 0, 3 },{ INF, INF, INF, INF, 7, 3, 0 }};floyd(matrix);}
}

《漫画算法2》源码整理-2 图算法相关推荐

  1. 《漫画算法》源码整理-7

    MyBitmap public class MyBitmap {//每一个word是一个long类型元素,对应64位二进制private long[] words;//bitmap的位数大小priva ...

  2. 《漫画算法》源码整理-5 排序算法

    冒泡排序 import java.util.Arrays;public class BubbleSort {public static void sort(int array[]){int tmp = ...

  3. 《漫画算法》源码整理-6

    判断链表有环 public class LinkedListCycle {/*** 判断是否有环* @param head 链表头节点*/public static boolean isCycle(N ...

  4. 《漫画算法》源码整理-4 大顶堆 小顶堆 优先队列

    堆操作 import java.util.Arrays;public class HeapOperator {/*** 上浮调整* @param array 待调整的堆*/public static ...

  5. 《漫画算法》源码整理-3 二叉树遍历

    二叉树前序 中序 后序 遍历 import java.util.Arrays; import java.util.LinkedList;public class BinaryTreeTraversal ...

  6. 《漫画算法》源码整理-2 数组 链表 队列

    数组操作 public class MyArray {private int[] array;private int size;public MyArray(int capacity){this.ar ...

  7. 《漫画算法》源码整理-1 时间复杂度 空间复杂度

    时间复杂度 public class TimeComplex {void eat1(int n){for(int i=0; i<n; i++){;System.out.println(" ...

  8. java计算机毕业设计漫画网站系统源码+系统+数据库+lw文档+mybatis+运行部署

    java计算机毕业设计漫画网站系统源码+系统+数据库+lw文档+mybatis+运行部署 java计算机毕业设计漫画网站系统源码+系统+数据库+lw文档+mybatis+运行部署 本源码技术栈: 项目 ...

  9. SURF算法与源码分析、下

    FROM: http://www.cnblogs.com/ronny/p/4048213.html 上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV ...

最新文章

  1. 支持国内版Office 365的PowerShell模块现已发布
  2. const reference const
  3. python整除表达 mod_[零基础学python]啰嗦的除法
  4. CDOJ 1070 秋实大哥打游戏 带权并查集
  5. 确定一组矩形是否有两个重叠的算法
  6. 笔记-项目人力资源管理-建设项目团队
  7. JS - Class继承
  8. vscode终端进程已终止 - 问题采集
  9. 快速排序中pivot的选取策略
  10. MongoDB副本集学习(三):性能和优化相关
  11. 数据结构课程设计题目
  12. 微信小程序云数据库增删改查
  13. ARM Cortex-M0系统简介
  14. 微信php视频怎么变成mp4,缓存下来的视频如何变成mp4格式?
  15. Caused by: No object in the CompoundRoot has a publicly accessible property named 'y' (no setter cou
  16. Cython简单demo
  17. 名单公布!支持这些高校,冲“双一流”
  18. Prompt Engineering 入门(二)
  19. 什么是promise,promise的用法。
  20. 微型计算机的多级存储结构,微型计算机存储系统结构.doc

热门文章

  1. Redis 的源码分析
  2. 极速发展的饿了么订单系统架构演进--转
  3. Spring源码解析之:Spring Security启动细节和工作模式--转载
  4. spring mvc DispatcherServlet详解之前传---FrameworkServlet
  5. 【SQL】SQL语句多表联合查询
  6. 详细的mongo工具手册,包含常见CURD+条件操作+聚合+案例
  7. 广告点击率预测 [离线部分]
  8. 高德地图横屏不显示服务器,高德地图不能横屏!
  9. html5 progress css,CSS content: attr() on HTML5 progress doesn't work
  10. [搜索]字符串的相似度问题-从编程之美说起