General Algorithms - Graph

2024-09-03 20:48
文章标签 graph general algorithms

本文主要是介绍General Algorithms - Graph,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • BFS
        • Red Knights Shortest Path - World CodeSprint 12 -
  • DFS
        • Even Tree
        • Roads and Libraries
  • MST
        • Kruskal MST Really Special Subtree
  • A

BFS

Red Knight’s Shortest Path - World CodeSprint 12 -

While loop 和 Recusive function可以互相用来替换; 0 为任意随机起始点, 4 为任意终点, 1,2,3为过程中步骤

DFS

Even Tree

  • 由于已知node范围在2-100,把adjacency list(vector)的范围直接写成 ”>100” 大一点的可以帮助更快确定位置。Sparse 的这种不确定分布的图,使用matrix会增加storage的负担,最大情况会需要一个100x100的matrix, 然而不是每个matrix 的cell都会用到。

  • 整个DFS里,不用一个一个node来回测试相连为不为even的tree,只用计算出DFS 遍历的nodes的和能不能被%2。使用公共的Variable来 counts 可以砍掉的edges的数量。

  • 最要小心是return的时候,nodes的总和计算要加上当前的这一个node (1), 即:dfs(当前node)+1。dfs(当前node) recursion (back tracking)的作用是累计当前node的 子nodes 的个数。

  • num_vertex+=num_nodes 累计到当前node,需要经过的子nodes.

  • Visit[] 的boolean判断在DFS非要不可,是为了track node.

Roads and Libraries

这里写图片描述

  • 此题已知edge 和 node统一Weights,则BFS没有优势但也可以用

  • 除了BFS, DFS这些能够确保每个点和边都会经过的,还有Minimum Spanning Tree 可以用. MST运用了Union-Find的Data structure,比如根据Weight来Swap

MST解法 1

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;class MinSpanningTree {public static final class Edge implements Comparable<Edge> {final int srcId;final int trgId;final long value;Edge(int srcId, int trgId, long value) {this.srcId = srcId;this.trgId = trgId;this.value = value;}public int compareTo(Edge o) {return (value < o.value) ? -1 : (value > o.value) ? 1 : 0;}}private static final class NodeEntry {NodesSet owner;NodeEntry next;     }private static final class NodesSet {int size;NodeEntry first;NodeEntry last;NodesSet(NodeEntry node) {size = 1;first = node;last = node;}static void merge(NodesSet set1, NodesSet set2) {if (set1.size < set2.size) {merge(set2, set1);return;}set1.size += set2.size;set1.last.next = set2.first;set1.last = set2.last;NodeEntry node = set2.first;while (node != null) {node.owner = set1;node = node.next;}}}private int nodesCount;private int edgesCount;private Edge[] edges;public MinSpanningTree(int nodes) {this.nodesCount = nodes;this.edgesCount = 0;this.edges = new Edge[nodesCount];}public void addEdge(int srcId, int trgId, long value) {if (edgesCount == edges.length) {edges = Arrays.copyOf(edges, 2 * edges.length);}edges[edgesCount++] = new Edge(srcId, trgId, value);}public Edge[] getMinSpanningTree() {NodeEntry[] nodes = new NodeEntry[nodesCount];for (int i = 0; i < nodesCount; i++) {nodes[i] = new NodeEntry();nodes[i].owner = new NodesSet(nodes[i]);}edges = Arrays.copyOf(edges, edgesCount);Arrays.sort(edges, 0, edgesCount);Edge[] spanningTree = new Edge[nodesCount-1];int k = 0;for (int i = 0; i < edgesCount & k+1 < nodesCount; i++) {NodesSet set1 = nodes[edges[i].srcId].owner;NodesSet set2 = nodes[edges[i].trgId].owner;if (set1 != set2) {NodesSet.merge(set1, set2);spanningTree[k++] = edges[i];}}return (k+1 < nodesCount) ? null : spanningTree;}public long getMinSpanningTreeSize() {Edge[] spanningTree = getMinSpanningTree();if (spanningTree == null) return -1;long treeSize = 0;for (Edge edge : spanningTree) treeSize += edge.value;return treeSize;}
}public class Solution {public static void main(String[] args) {Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in), 256 << 10));int testsNumber = in.nextInt();for (int test = 0; test < testsNumber; test++) {int n = in.nextInt();int m = in.nextInt();int costOfLib = in.nextInt();int costOfRoad = in.nextInt();MinSpanningTree tree = new MinSpanningTree(n+1);for (int i = 0; i < n; i++) {tree.addEdge(0, i+1, costOfLib);}for (int i = 0; i < m; i++) {int v1 = in.nextInt();int v2 = in.nextInt();tree.addEdge(v1, v2, costOfRoad);}System.out.println(tree.getMinSpanningTreeSize());}in.close();}
}

MST解法2

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;public class Solution {public static class Edge {public int u, v;public long w;public Edge(int u, int v, long w) {this.u = u;this.v = v;this.w = w;}}public static class UnionFind {private int parent[];private int rank[];public UnionFind(int n) {parent = new int[n];rank = new int[n];for (int i = 0; i < n; i++) {parent[i] = i;}}public int findSet(int x) {while (parent[x] != x) {parent[x] = parent[parent[x]];x = parent[x];}return x;}public void union(int a, int b) {int setA = findSet(a);int setB = findSet(b);if (rank[setA] > rank[setB]) {parent[setB] = setA;} else {parent[setA] = setB;if (rank[setA] == rank[setB]) {rank[setB] += 1;}}}public boolean connected(int a, int b) {return (findSet(a) == findSet(b));}}public static void main(String[] args) {Scanner in = new Scanner(System.in);int q = in.nextInt();for(int a0 = 0; a0 < q; a0++){long n = in.nextLong();long m = in.nextLong();long lib = in.nextLong();long road = in.nextLong();List<Edge> edges = new ArrayList<>();for(int a1 = 0; a1 < m; a1++){int city_1 = in.nextInt();int city_2 = in.nextInt();edges.add(new Edge(city_1, city_2, road));}UnionFind uf = new UnionFind((int)(n + 1));long minCost = n * lib;long roadCosts = 0;long libCosts = n * lib;for (Edge edge : edges) {if (!uf.connected(edge.u, edge.v)) {uf.union(edge.u, edge.v);roadCosts += road;libCosts -= lib;minCost = Math.min(minCost, roadCosts + libCosts);}}System.out.println(minCost);}}
}

MST

Kruskal (MST): Really Special Subtree

这里写图片描述

按照算法,右图的大括号里的已经是所有edges从小到大的排序。已知从最小的那个开始直到所有nodes都包含在一个Minimum Spanning Tree里:先是Edge(1,3), 到Edge(2,3),当到Edge(1,2)时,由于前面的两个Edges已经包含了1 与 2 这两个node,所以忽略Edge(1,2);到Edge(3,4), 然后到Edge(1,4),由于前面也已经包含了Node 1 和 4,所以忽略Edge(1,4), 到Edge(2,4)同理,也被忽略。则最后只留有:

Edge(1,3) +Edge(2,3)+Edge(3,4) = 3+4+5 = 12

  • 这个算法的time complexity 由Sort来决定。因为在找MST时的For loop只为Edge的数量级,很少很简单。

A*

这篇关于General Algorithms - Graph的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134010

相关文章

图神经网络框架DGL实现Graph Attention Network (GAT)笔记

参考列表: [1]深入理解图注意力机制 [2]DGL官方学习教程一 ——基础操作&消息传递 [3]Cora数据集介绍+python读取 一、DGL实现GAT分类机器学习论文 程序摘自[1],该程序实现了利用图神经网络框架——DGL,实现图注意网络(GAT)。应用demo为对机器学习论文数据集——Cora,对论文所属类别进行分类。(下图摘自[3]) 1. 程序 Ubuntu:18.04

SIGMOD-24概览Part7: Industry Session (Graph Data Management)

👇BG3: A Cost Effective and I/O Efficient Graph Database in ByteDance 🏛机构:字节 ➡️领域: Information systems → Data management systemsStorage management 📚摘要:介绍了字节新提出的ByteGraph 3.0(BG3)模型,用来处理大规模图结构数据 背景

神经网络训练不起来怎么办(零)| General Guidance

摘要:模型性能不理想时,如何判断 Model Bias, Optimization, Overfitting 等问题,并以此着手优化模型。在这个分析过程中,我们可以对Function Set,模型弹性有直观的理解。关键词:模型性能,Model Bias, Optimization, Overfitting。 零,领域背景 如果我们的模型表现较差,那么我们往往需要根据 Training l

Study Plan For Algorithms - Part24

1. 包含min函数的栈 定义栈的数据结构,要求在该类型中实现一个 min 函数,能够获取栈的最小元素。在该栈中,调用 min、push 以及 pop 函数的时间复杂度均为 O (1)。 方法: class MinStack:def __init__(self):self.stack = []self.min_stack = [float('inf')]def push(self, x):sel

A Comprehensive Survey on Graph Neural Networks笔记

一、摘要-Abstract 1、传统的深度学习模型主要处理欧几里得数据(如图像、文本),而图神经网络的出现和发展是为了有效处理和学习非欧几里得域(即图结构数据)的信息。 2、将GNN划分为四类:recurrent GNNs(RecGNN), convolutional GNNs,(GCN), graph autoencoders(GAE), and spatial–temporal GNNs(S

Neighborhood Homophily-based Graph Convolutional Network

#paper/ccfB 推荐指数: #paper/⭐ #pp/图结构学习 流程 重定义同配性指标: N H i k = ∣ N ( i , k , c m a x ) ∣ ∣ N ( i , k ) ∣ with c m a x = arg ⁡ max ⁡ c ∈ [ 1 , C ] ∣ N ( i , k , c ) ∣ NH_i^k=\frac{|\mathcal{N}(i,k,c_{

torch.backends.cudnn.benchmark和torch.use_deterministic_algorithms总结学习记录

经常使用PyTorch框架的应该对于torch.backends.cudnn.benchmark和torch.use_deterministic_algorithms这两个语句并不陌生,在以往开发项目的时候可能专门化花时间去了解过,也可能只是浅尝辄止简单有关注过,正好今天再次遇到了就想着总结梳理一下。 torch.backends.cudnn.benchmark 是 PyTorch 中的一个设置

boost.graph之属性

相关宏 BOOST_INSTALL_PROPERTY #define BOOST_INSTALL_PROPERTY(KIND, NAME) \template <> struct property_kind<KIND##_##NAME##_t> { \typedef KIND##_property_tag type; \} 最终形式为 template <> struct proper

【ZOJ】3874 Permutation Graph 【FFT+CDQ分治】

传送门:【ZOJ】3874 Permutation Graph 题目分析: 容易知道一个个连通块内部的标号都是连续的,否则一定会有另一个连通块向这个连通块建边,或者这个连通块向另一个连通块建边。而且从左到右左边的连通块内最大的标号小于右边连通块内最小的标号。 然后我们可以构造dp方程: dp[n]=n!−i!∗dp[n−i] \qquad \qquad dp[n] = n! - i! *

【HDU】5333 Undirected Graph【LCT+BIT】

传送门:【HDU】5333 Undirected Graph my  code: my~~code: #pragma comment(linker, "/STACK:1024000000")#include <stdio.h>#include <string.h>#include <map>#include <algorithm>using namespace std ;typed