SNAP Library 2.3, User Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
subgraph.cpp
Go to the documentation of this file.
1 namespace TSnap {
2 
4 // Graph Algorithms
5 
6 // RenumberNodes ... Renumber node ids in the subgraph to 0...N-1
7 PUNGraph GetSubGraph(const PUNGraph& Graph, const TIntV& NIdV, const bool& RenumberNodes) {
8  //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
9  PUNGraph NewGraphPt = TUNGraph::New();
10  TUNGraph& NewGraph = *NewGraphPt;
11  NewGraph.Reserve(NIdV.Len(), -1);
12  TIntSet NIdSet(NIdV.Len());
13  for (int n = 0; n < NIdV.Len(); n++) {
14  if (Graph->IsNode(NIdV[n])) {
15  NIdSet.AddKey(NIdV[n]);
16  if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
17  else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
18  }
19  }
20  if (! RenumberNodes) {
21  for (int n = 0; n < NIdSet.Len(); n++) {
22  const int SrcNId = NIdSet[n];
23  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
24  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
25  const int OutNId = NI.GetOutNId(edge);
26  if (NIdSet.IsKey(OutNId)) {
27  NewGraph.AddEdge(SrcNId, OutNId); }
28  }
29  }
30  } else {
31  for (int n = 0; n < NIdSet.Len(); n++) {
32  const int SrcNId = NIdSet[n];
33  const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
34  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
35  const int OutNId = NI.GetOutNId(edge);
36  if (NIdSet.IsKey(OutNId)) {
37  NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
38  }
39  }
40  }
41  return NewGraphPt;
42 }
43 
44 // RenumberNodes ... Renumber node ids in the subgraph to 0...N-1
45 PNGraph GetSubGraph(const PNGraph& Graph, const TIntV& NIdV, const bool& RenumberNodes) {
46  //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
47  PNGraph NewGraphPt = TNGraph::New();
48  TNGraph& NewGraph = *NewGraphPt;
49  NewGraph.Reserve(NIdV.Len(), -1);
50  TIntSet NIdSet(NIdV.Len());
51  for (int n = 0; n < NIdV.Len(); n++) {
52  if (Graph->IsNode(NIdV[n])) {
53  NIdSet.AddKey(NIdV[n]);
54  if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
55  else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
56  }
57  }
58  if (! RenumberNodes) {
59  for (int n = 0; n < NIdSet.Len(); n++) {
60  const int SrcNId = NIdSet[n];
61  const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
62  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
63  const int OutNId = NI.GetOutNId(edge);
64  if (NIdSet.IsKey(OutNId)) {
65  NewGraph.AddEdge(SrcNId, OutNId); }
66  }
67  }
68  } else {
69  for (int n = 0; n < NIdSet.Len(); n++) {
70  const int SrcNId = NIdSet[n];
71  const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
72  for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
73  const int OutNId = NI.GetOutNId(edge);
74  if (NIdSet.IsKey(OutNId)) {
75  NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
76  }
77  }
78  }
79  return NewGraphPt;
80 }
81 
82 PUNGraph GetEgonet(const PUNGraph& Graph, const int CtrNId, int& ArndEdges) {
83  PUNGraph NewGraphPt = TUNGraph::New();
84  TUNGraph& NewGraph = *NewGraphPt;
85  NewGraph.AddNode(CtrNId);
86  const TUNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
87  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
88  NewGraph.AddNode(CtrNode.GetInNId(i));
89  }
90  ArndEdges = 0;
91  for (int i = 0; i < CtrNode.GetInDeg(); ++i) {
92  int NbrNId = CtrNode.GetInNId(i);
93  const TUNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
94  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
95  int NbrNbrNId = NbrNode.GetInNId(j);
96  if (NewGraph.IsNode(NbrNbrNId)) {
97  if (!NewGraph.IsEdge(NbrNId, NbrNbrNId)) {
98  NewGraph.AddEdge(NbrNId, NbrNbrNId);
99  }
100  } else {
101  ArndEdges++;
102  }
103  }
104  }
105  return NewGraphPt;
106 }
107 
108 PNGraph GetEgonet(const PNGraph& Graph, const int CtrNId, int& InEdges, int& OutEdges) {
109  PNGraph NewGraphPt = TNGraph::New();
110  TNGraph& NewGraph = *NewGraphPt;
111  NewGraph.AddNode(CtrNId);
112  const TNGraph::TNodeI& CtrNode = Graph->GetNI(CtrNId);
113  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
114  NewGraph.AddNode(CtrNode.GetNbrNId(i));
115  }
116  InEdges = 0;
117  OutEdges = 0;
118  for (int i = 0; i < CtrNode.GetDeg(); ++i) {
119  int NbrNId = CtrNode.GetNbrNId(i);
120  const TNGraph::TNodeI& NbrNode = Graph->GetNI(NbrNId);
121  for (int j = 0; j < NbrNode.GetInDeg(); ++j) {
122  int NbrNbrNId = NbrNode.GetInNId(j);
123  if (NewGraph.IsNode(NbrNbrNId)) {
124  NewGraph.AddEdge(NbrNbrNId, NbrNId);
125  } else {
126  InEdges++;
127  }
128  }
129  for (int j = 0; j < NbrNode.GetOutDeg(); ++j) {
130  int NbrNbrNId = NbrNode.GetOutNId(j);
131  if (!NewGraph.IsNode(NbrNbrNId)) {
132  OutEdges++;
133  }
134  }
135  }
136  return NewGraphPt;
137 }
138 
139 } // namespace TSnap
int GetNbrNId(const int &NodeN) const
Returns ID of NodeN-th neighboring node.
Definition: graph.h:356
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:405
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:461
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:63
PUNGraph GetEgonet(const PUNGraph &Graph, const int CtrNId, int &ArndEdges)
Returns the egonet of node CtrNId as center in undirected graph Graph. And returns number of edges ar...
Definition: subgraph.cpp:82
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:208
int GetOutDeg() const
Returns out-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:86
Undirected graph.
Definition: graph.h:32
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:286
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:249
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:455
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:149
int AddKey(const TKey &Key)
Definition: shash.h:1248
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
Definition: graph.h:340
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:84
Directed graph.
Definition: graph.h:293
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:96
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:344
int GetInDeg() const
Returns in-degree of the current node (returns same as value GetDeg() since the graph is undirected)...
Definition: graph.h:84
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:324
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
Definition: graph.h:508
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:199
Definition: bd.h:196
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:342
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition: graph.cpp:108
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:348
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:352
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:91