当前位置: 首页 > news >正文

图论12-无向带权图及实现

文章目录

  • 带权图
    • 1.1带权图的实现
    • 1.2 完整代码

带权图

在这里插入图片描述

1.1带权图的实现

在无向无权图的基础上,增加边的权。

使用TreeMap存储边的权重。

  • 遍历输入文件,创建TreeMap adj存储每个节点。
  • 每个输入的adj节点链接新的TreeMap,存储相邻的边和权重
private TreeMap<Integer, Integer>[] adj;adj = new TreeMap[V];for(int i = 0; i < V; i ++)adj[i] = new TreeMap<Integer, Integer>();
  • 两条边相连,则分别把权重加入各自的邻接表中
adj[a].put(b, weight);
adj[b].put(a, weight);
  • 判断两点之间是否有边
public boolean hasEdge(int v, int w){validateVertex(v);validateVertex(w);return adj[v].containsKey(w);
}
  • 求相邻的所有节点
public Iterable<Integer> adj(int v){validateVertex(v);return adj[v].keySet();
}
  • 求两点的权值
public int getWeight(int v, int w){if(hasEdge(v, w)) return adj[v].get(w);throw new IllegalArgumentException(String.format("No edge %d-%d", v, w));
}
  • 移除边
public void removeEdge(int v, int w){validateVertex(v);validateVertex(w);if(adj[v].containsKey(w)) E --;adj[v].remove(w);adj[w].remove(v);
}
  • 复制一个图
public Object clone(){try{WeightedGraph cloned = (WeightedGraph) super.clone();cloned.adj = new TreeMap[V];for(int v = 0; v < V; v ++){cloned.adj[v] = new TreeMap<Integer, Integer>();for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())cloned.adj[v].put(entry.getKey(), entry.getValue());}return cloned;}catch (CloneNotSupportedException e){e.printStackTrace();}return null;
}

1.2 完整代码

package Chapter09_Weight_Graph;import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;/// 暂时只支持无向带权图
public class WeightedGraph implements Cloneable{private int V;private int E;private TreeMap<Integer, Integer>[] adj;public WeightedGraph(String filename){File file = new File(filename);try(Scanner scanner = new Scanner(file)){V = scanner.nextInt();if(V < 0) throw new IllegalArgumentException("V must be non-negative");adj = new TreeMap[V];for(int i = 0; i < V; i ++)adj[i] = new TreeMap<Integer, Integer>();E = scanner.nextInt();if(E < 0) throw new IllegalArgumentException("E must be non-negative");for(int i = 0; i < E; i ++){int a = scanner.nextInt();validateVertex(a);int b = scanner.nextInt();validateVertex(b);int weight = scanner.nextInt();if(a == b) throw new IllegalArgumentException("Self Loop is Detected!");if(adj[a].containsKey(b)) throw new IllegalArgumentException("Parallel Edges are Detected!");adj[a].put(b, weight);adj[b].put(a, weight);}}catch(IOException e){e.printStackTrace();}}public void validateVertex(int v){if(v < 0 || v >= V)throw new IllegalArgumentException("vertex " + v + "is invalid");}public int V(){return V;}public int E(){return E;}public boolean hasEdge(int v, int w){validateVertex(v);validateVertex(w);return adj[v].containsKey(w);}public Iterable<Integer> adj(int v){validateVertex(v);return adj[v].keySet();}public int getWeight(int v, int w){if(hasEdge(v, w)) return adj[v].get(w);throw new IllegalArgumentException(String.format("No edge %d-%d", v, w));}public int degree(int v){validateVertex(v);return adj[v].size();}public void removeEdge(int v, int w){validateVertex(v);validateVertex(w);if(adj[v].containsKey(w)) E --;adj[v].remove(w);adj[w].remove(v);}@Overridepublic Object clone(){try{WeightedGraph cloned = (WeightedGraph) super.clone();cloned.adj = new TreeMap[V];for(int v = 0; v < V; v ++){cloned.adj[v] = new TreeMap<Integer, Integer>();for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())cloned.adj[v].put(entry.getKey(), entry.getValue());}return cloned;}catch (CloneNotSupportedException e){e.printStackTrace();}return null;}@Overridepublic String toString(){StringBuilder sb = new StringBuilder();sb.append(String.format("V = %d, E = %d\n", V, E));for(int v = 0; v < V; v ++){sb.append(String.format("%d : ", v));for(Map.Entry<Integer, Integer> entry: adj[v].entrySet())sb.append(String.format("(%d: %d) ", entry.getKey(), entry.getValue()));sb.append('\n');}return sb.toString();}public static void main(String[] args){WeightedGraph g = new WeightedGraph("gw1.txt");System.out.print(g);}
}

在这里插入图片描述

http://www.lryc.cn/news/226644.html

相关文章:

  • 每日一题(LeetCode)----数组--有序数组的平方
  • SpringCloud微服务:Eureka
  • 19.删除链表的倒数第N个结点(LeetCode)
  • PyTorch技术和深度学习——三、深度学习快速入门
  • 360导航恶意修改浏览器启动页!我的chrome和IE均中招,如何解决?
  • RabbitMQ的高级特性
  • Java自学第10课:JavaBean和servlet基础
  • AR打卡小程序:构建智能办公的新可能
  • Python环境安装、Pycharm开发工具安装(IDE)
  • 报时机器人的rasa shell执行流程分析
  • C#开发的OpenRA游戏之世界存在的属性UpdatesPlayerStatistics(2)
  • Ocelot:.NET开源API网关提供路由管理、服务发现、鉴权限流等功能
  • wsl [Ubuntu20.04.6] 安装 Hadoop
  • 2023华为ict网络赛道初赛(部分)试题
  • rabbitMq虚拟主机概念
  • 2-CentOS7.9下安装docker
  • 【已验证-直接用】微信小程序wx.request请求服务器json数据并渲染到页面
  • 如何提高小红书笔记的互动率
  • RabbitMQ 系列教程
  • 无感刷新token
  • 【Python大数据笔记_day06_Hive】
  • Netty--文件编程
  • SVN 服务器建立
  • iPhone或在2024开放第三方应用商店。
  • 《C和指针》笔记36:动态内存分配
  • C/S架构学习之基于UDP的本地通信(服务器)
  • excel如何加密(excel加密的三种方法)
  • 玩了个锤子游戏小程序搭建流程:探索深度与逻辑的结合
  • 召回率计算及影响因素
  • 在Qt中怎么由函数定义自动创建函数实现模板