首页 > 编程语言 >Snap算法学习01-03Snap中的类及其定义

Snap算法学习01-03Snap中的类及其定义

时间:2023-07-02 16:00:45浏览次数:51  
标签:01 const int graph TNEGraph 03Snap Snap return EId

 

 


 

 

//graph.h定义的基本类型

无向图  

/// Undirected graph. ##TUNGraph::Class
class TUNGraph

 

有向图

/// Directed graph. ##TNGraph::Class
class TNGraph

 

二部图

/// Bipartite graph. ##Bipartite_graph
class TBPGraph

 

多重图

/// Directed multigraph. ##TNEGraph::Class
class TNEGraph

 

 

 下面以多重图为例查看类型  

  1 class TNEGraph   
2 public: 
3 typedef TNEGraph TNet;
4 typedef TPt<TNEGraph> PNet; 5 public: 6 class TNode {//节点类型定义;并friend class TNEGraph}; 7 class TEdge {//边的类型定义;并friend class TNEGraph}; 8 class TNodeI {//节点迭代器类型定义;并友元TNEGraph }; 9 class TEdgeI {//边迭代器类型定义;并友元TNEGraph }; 10 private: 11 TNode& GetNode(const int& NId) { return NodeH.GetDat(NId); } 12 const TNode& GetNode(const int& NId) const { return NodeH.GetDat(NId); } 13 TEdge& GetEdge(const int& EId) { return EdgeH.GetDat(EId); } 14 const TEdge& GetEdge(const int& EId) const { return EdgeH.GetDat(EId); } 15 private: 16 TCRef CRef; 17 TInt MxNId, MxEId; 18 THash<TInt, TNode> NodeH; 19 THash<TInt, TEdge> EdgeH; 20 public: 21 TNEGraph() : CRef(), MxNId(0), MxEId(0) { } 22 /// Constructor that reserves enough memory for a graph of Nodes nodes and Edges edges. 23 explicit TNEGraph(const int& Nodes, const int& Edges) : CRef(), MxNId(0), MxEId(0)
{ Reserve(Nodes, Edges); } 24 TNEGraph(const TNEGraph& Graph) :
MxNId(Graph.MxNId), MxEId(Graph.MxEId), NodeH(Graph.NodeH), EdgeH(Graph.EdgeH) { } 25 /// Constructor for loading the graph from a (binary) stream SIn. 26 TNEGraph(TSIn& SIn) : MxNId(SIn), MxEId(SIn), NodeH(SIn), EdgeH(SIn) { } 27 /// Saves the graph to a (binary) stream SOut. 28 void Save(TSOut& SOut) const
{ MxNId.Save(SOut); MxEId.Save(SOut); NodeH.Save(SOut); EdgeH.Save(SOut);} 29 /// Static constructor that returns a pointer to the graph.
///Call: PNEGraph Graph = TNEGraph::New(). 30 static PNEGraph New() { return PNEGraph(new TNEGraph()); } 31 /// Static constructor that returns a pointer to the graph and reserves enough memory for
///memory for Nodes nodes and Edges edges. ##TNEGraph::New
32 static PNEGraph New(const int& Nodes, const int& Edges)
{ return PNEGraph(new TNEGraph(Nodes, Edges));}
33 // Static constructor that loads the graph from a stream SIn and returns a pointer to it.
 34   static PNEGraph Load(TSIn& SIn) { return PNEGraph(new TNEGraph(SIn)); }
 35   /// Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
 36   bool HasFlag(const TGraphFlag& Flag) const;
 37   TNEGraph& operator = (const TNEGraph& Graph) { if (this!=&Graph) {
 38     MxNId=Graph.MxNId; MxEId=Graph.MxEId; NodeH=Graph.NodeH; EdgeH=Graph.EdgeH; }
return *this; } 39 40 /// Returns the number of nodes in the graph. 41 int GetNodes() const { return NodeH.Len(); } 42 /// Adds a node of ID NId to the graph. ##TNEGraph::AddNode 43 int AddNode(int NId = -1); 44 /// Adds a node of ID NodeI.GetId() to the graph. 45 int AddNode(const TNodeI& NodeId) { return AddNode(NodeId.GetId()); } 46 /// Deletes node of ID NId from the graph. ##TNEGraph::DelNode 47 void DelNode(const int& NId); 48 /// Deletes node of ID NodeI.GetId() from the graph. 49 void DelNode(const TNode& NodeI) { DelNode(NodeI.GetId()); } 50 /// Tests whether ID NId is a node. 51 bool IsNode(const int& NId) const { return NodeH.IsKey(NId); } 52 /// Returns an iterator referring to the first node in the graph. 53 TNodeI BegNI() const { return TNodeI(NodeH.BegI(), this); } 54 /// Returns an iterator referring to the past-the-end node in the graph. 55 TNodeI EndNI() const { return TNodeI(NodeH.EndI(), this); } 56 /// Returns an iterator referring to the node of ID NId in the graph. 57 TNodeI GetNI(const int& NId) const { return TNodeI(NodeH.GetI(NId), this); } 58 /// Returns an ID that is larger than any node ID in the graph. 59 int GetMxNId() const { return MxNId; } 60 61 /// Returns the number of edges in the graph. 62 int GetEdges() const { return EdgeH.Len(); } 63 /// Adds an edge with ID EId between node IDs SrcNId and DstNId to the graph.
/// ##TNEGraph::AddEdge 64 int AddEdge(const int& SrcNId, const int& DstNId, int EId = -1); 65 /// Adds an edge between EdgeI.GetSrcNId() and EdgeI.GetDstNId() to the graph. 66 int AddEdge(const TEdgeI& EdgeI)
{ return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI.GetId()); } 67 /// Deletes an edge with edge ID EId from the graph. 68 void DelEdge(const int& EId); 69 /// Deletes all edges between node IDs SrcNId and DstNId from the graph.
///##TNEGraph::DelEdge 70 void DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true); 71 /// Tests whether an edge with edge ID EId exists in the graph. 72 bool IsEdge(const int& EId) const { return EdgeH.IsKey(EId); } 73 /// Tests whether an edge between node IDs SrcNId and DstNId exists in the graph. 74 bool IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir = true) const
{ int EId; return IsEdge(SrcNId, DstNId, EId, IsDir); } 75 /// Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
/// if an edge exists, return its edge ID in EId 76 bool IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir = true)
const; 77 /// Returns an edge ID between node IDs SrcNId and DstNId,
/// if such an edge exists. Otherwise, return -1. 78 int GetEId(const int& SrcNId, const int& DstNId) const
{ int EId; return IsEdge(SrcNId, DstNId, EId)?EId:-1; } 79 /// Returns an iterator referring to the first edge in the graph. 80 TEdgeI BegEI() const { return TEdgeI(EdgeH.BegI(), this); } 81 /// Returns an iterator referring to the past-the-end edge in the graph. 82 TEdgeI EndEI() const { return TEdgeI(EdgeH.EndI(), this); } 83 /// Returns an iterator referring to edge with edge ID EId. 84 TEdgeI GetEI(const int& EId) const { return TEdgeI(EdgeH.GetI(EId), this); } 85 /// Returns an iterator referring to edge (SrcNId, DstNId) in the graph. 86 TEdgeI GetEI(const int& SrcNId, const int& DstNId) const
{ return GetEI(GetEId(SrcNId, DstNId)); } 87 88 /// Returns an ID of a random node in the graph. 89 int GetRndNId(TRnd& Rnd=TInt::Rnd) { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); } 90 /// Returns an interator referring to a random node in the graph. 91 TNodeI GetRndNI(TRnd& Rnd=TInt::Rnd) { return GetNI(GetRndNId(Rnd)); } 92 /// Returns an ID of a random edge in the graph. 93 int GetRndEId(TRnd& Rnd=TInt::Rnd) { return EdgeH.GetKey(EdgeH.GetRndKeyId(Rnd, 0.8)); } 94 /// Returns an interator referring to a random edge in the graph. 95 TEdgeI GetRndEI(TRnd& Rnd=TInt::Rnd) { return GetEI(GetRndEId(Rnd)); } 96 /// Gets a vector IDs of all nodes in the graph. 97 void GetNIdV(TIntV& NIdV) const; 98 /// Gets a vector IDs of all edges in the graph. 99 void GetEIdV(TIntV& EIdV) const; 100 101 /// Tests whether the graph is empty (has zero nodes). 102 bool Empty() const { return GetNodes()==0; } 103 /// Deletes all nodes and edges from the graph. 104 void Clr() { MxNId=0; MxEId=0; NodeH.Clr(); EdgeH.Clr(); } 105 /// Reserves memory for a graph of Nodes nodes and Edges edges. 106 void Reserve(const int& Nodes, const int& Edges) { 107 if (Nodes>0) { NodeH.Gen(Nodes/2); } if (Edges>0) { EdgeH.Gen(Edges/2); } } 108 /// Defragments the graph. ##TNEGraph::Defrag 109 void Defrag(const bool& OnlyNodeLinks=false); 110 /// Checks the graph data structure for internal consistency. ##TNEGraph::IsOk 111 bool IsOk(const bool& ThrowExcept=true) const; 112 /// Print the graph in a human readable form to an output stream OutF. 113 void Dump(FILE *OutF=stdout) const; 114 /// Returns a small multigraph on 5 nodes and 6 edges. ##TNEGraph::GetSmallGraph 115 static PNEGraph GetSmallGraph(); 116 friend class TPt<TNEGraph>; 117 }; 118 119 // set flags 120 namespace TSnap { 121 template <> struct IsMultiGraph<TNEGraph> { enum { Val = 1 }; }; 122 template <> struct IsDirected<TNEGraph> { enum { Val = 1 }; }; 123 }

 

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

 

标签:01,const,int,graph,TNEGraph,03Snap,Snap,return,EId
From: https://www.cnblogs.com/yuweng1689/p/17520872.html

相关文章

  • P7316 [COCI2018-2019#3] NLO
    考虑延续GDKOI普及组签到题的做法。先枚举\((x,y)\)考虑他会更新哪些节点,那么这个在GDKOI上是体现在一个差分上面。这里\(n\)很大而\(k\)很小,那么我们就可以考虑枚举\(n\)和\(k\),但是使用线段树来做。但是注意到一个事情,我们做区间赋值附的不是简单的$0/1$......
  • P5471 [NOI2019] 弹跳
    我只会签到题.jpg。显然可以使用二维线段树优化建图拿到一定的部分分,但是这并不优秀。考虑从值域上来入手dijkstra。看做是装置间的最短路顺带更新节点,那么我们可以写一个树套树来维护这一些待更新的点,因为dist是递增的,所以可以更新后删去这些点,然后就可以\(n\logn\)的空......
  • P3519 [POI2011]ROZ-Difference
    考虑枚举最大的字母所处的位置\(i\)作为端点和最小的字母\(j\)。然后就有记录一下前缀出现次数\(cnt\),枚举一个区间。\[cnt_{i,ch_i}-cnt_{i,j}-(cnt_{i',ch_i}-cnt_{i',j})\]求这个式子最大值。显然这两个式子相似,记录一下关于\(ch_i\)的\(cnt\)前缀最小值即......
  • [NOIP2015 提高组] 跳石头
    [NOIP2015提高组]跳石头题目背景一年一度的“跳石头”比赛又要开始了!题目描述这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有\(N\)块岩石(不含起点和终点的岩石)。在比赛过程中,选手们将从......
  • OpenGL环境配置 和 测试代码(GLEW/GLFW VS2019)
    1.配置 2.测试代码:2.1 在黑板色的窗口上画一个三角形1#include<iostream>23//GLEW4#defineGLEW_STATIC5#include<GL/glew.h>67//GLFW8#include<GLFW/glfw3.h>91011//Functionprototypes12voidkey_callback(GLFWw......
  • P1587 [NOI2016] 循环之美
    题意给定\(n,m,k\)(\(1\len,m\le10^9\),)问在\(k\)进制下有多少个分数值不同的\(\frac{x}{y}\)满足\(x\len,y\lem\)且其小数形式的循环节从小数点后第一位开始。sol因为要求不同分数值,我们只考虑既约分数。类比十进制,故要求\(gcd(y,k)=1\)。所以答案为:\[\sum_{i=1}......
  • 光脚丫学ASP.NET MVC(0001):创建ASP.NET MVC应用程序项目
    演示视频:http://u.115.com/file/cloa5hpg本演示介绍了如何使用VisualStudio2010创建ASP.NETMVC应用程序项目。下面是VS的新建项目对话框。其中红色线圈住的两个模板是VS2010安装时就自带的ASP.NETMVC2的项目模板,可以从官方网站下载最新的项目模板。下图是新建单元测试项目对话......
  • N层研习记录01:试图通过Boolean参数控制并发冲突的检查方式(LINQ to SQL)
    作者:光脚丫思考如果你只想快速的查看测试代码的主题部分,或者想更具体的了解测试的详细记录,则可以参看另一篇名为《N层研习中的测试代码01》的博文,地址是:微软MSDN带来的困惑微软的MSDN确实是个好东西,其中提供的技术资料,只要你善于去学习,必定是会受益匪浅的。毕竟,技术是微软的,它所......
  • N层研习中的测试代码01
    这是前文《N层研习记录01:试图通过Boolean参数控制并发冲突的检查方式(LINQtoSQL)》用到的测试代码。只是包含了其中最重要的部分,如果要想获取完整的代码,可以通过以下地址进行下载:下载地址2:http://u.115.com/file/f26716bcc2以上地址如果均不能下载,请留言通知我!测试代码的服务端使......
  • [MEF]第01篇 MEF使用入门
    一、演示概述此演示初步介绍了MEF的基本使用,包括对MEF中的Export、Import和Catalog做了初步的介绍,并通过一个具体的Demo来展示MEF是如何实现高内聚、低耦合和高扩展性的软件架构。演示中,针对于IBookService接口,有3个不同版本的实现,分别是ComputerBookServiceImp、HistoryBookSer......