18 template<
class PGraph> PGraph
GetSubGraph(
const PGraph& Graph,
const TIntV& NIdV);
45 template<
class PGraph> PGraph
GetESubGraph(
const PGraph& Graph,
const TIntV& EIdV);
64 template<
class PGraph,
class TEdgeDat> PGraph
GetEDatSubGraph(
const PGraph& Graph,
const TEdgeDat& EDat,
const int&
Cmp);
80 template<
class PGraph,
class TEdgeDat> PGraph
GetEDatSubGraph(
const PGraph& Graph,
const TIntV& NIdV,
const TEdgeDat& EDat,
const int&
Cmp);
95 template<
class POutGraph,
class PInGraph> POutGraph
ConvertGraph(
const PInGraph& InGraph,
const bool& RenumberNodes=
false);
108 template<
class POutGraph,
class PInGraph> POutGraph
ConvertSubGraph(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes=
false);
122 template<
class POutGraph,
class PInGraph> POutGraph
ConvertESubGraph(
const PInGraph& InGraph,
const TIntV& EIdV,
const bool& RenumberNodes=
false);
130 template<
class PGraph> PGraph
GetRndSubGraph(
const PGraph& Graph,
const int& NNodes);
135 template<
class PGraph> PGraph
GetRndESubGraph(
const PGraph& Graph,
const int& NEdges);
145 namespace TSnapDetail {
147 template <
class PGraph,
bool IsMultiGraph>
149 static PGraph
Do(
const PGraph& Graph,
const TIntV& NIdV) {
150 PGraph NewGraphPt = PGraph::TObj::New();
151 typename PGraph::TObj& NewGraph = *NewGraphPt;
152 NewGraph.Reserve(NIdV.
Len(), -1);
153 for (
int n = 0; n < NIdV.
Len(); n++) {
154 if (Graph->IsNode(NIdV[n])) {
155 NewGraph.AddNode(Graph->GetNI(NIdV[n])); }
157 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
158 if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId())) {
159 NewGraph.AddEdge(EI); }
166 template <
class PGraph>
168 static PGraph
Do(
const PGraph& Graph,
const TIntV& NIdV) {
170 PGraph NewGraphPt = PGraph::TObj::New();
171 typename PGraph::TObj& NewGraph = *NewGraphPt;
172 NewGraph.Reserve(NIdV.
Len(), -1);
174 for (
int n = 0; n < NIdV.
Len(); n++) {
176 if (Graph->IsNode(NIdV[n])) { NewGraph.AddNode(NIdV[n]); NodeSet.
AddKey(NIdV[n]); } }
178 if (Graph->IsNode(NIdV[n])) { NewGraph.AddNode(Graph->GetNI(NIdV[n])); NodeSet.
AddKey(NIdV[n]); } }
180 for (
int n = 0; n < NodeSet.
Len(); n++) {
181 const int SrcNId = NodeSet[n];
182 const typename PGraph::TObj::TNodeI NI = Graph->GetNI(SrcNId);
184 const int OutNId = NI.GetOutNId(
edge);
185 if (NewGraph.IsNode(OutNId)) {
187 NewGraph.AddEdge(SrcNId, OutNId); }
189 NewGraph.AddEdge(Graph->GetEI(SrcNId, OutNId)); }
199 template<
class PGraph>
205 template<
class PGraph>
208 PGraph NewGraphPt = PGraph::TObj::New();
209 typename PGraph::TObj& NewGraph = *NewGraphPt;
210 NewGraph.Reserve(-1, EIdV.
Len());
212 const int EId = EIdV[
edge];
214 const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(EId);
215 if (! NewGraph.IsNode(EI.GetSrcNId())) {
216 NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
218 if (! NewGraph.IsNode(EI.GetDstNId())) {
219 NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
221 NewGraph.AddEdge(EI);
226 template<
class PGraph>
228 PGraph NewGraphPt = PGraph::TObj::New();
229 typename PGraph::TObj& NewGraph = *NewGraphPt;
230 NewGraph.Reserve(-1, EdgeV.
Len());
232 const int SrcNId = EdgeV[
edge].Val1;
233 const int DstNId = EdgeV[
edge].Val2;
234 const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(SrcNId, DstNId);
235 if (! NewGraph.IsNode(EI.GetSrcNId())) {
236 NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
238 if (! NewGraph.IsNode(EI.GetDstNId())) {
239 NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
241 NewGraph.AddEdge(EI);
247 template<
class PGraph,
class TEdgeDat>
250 PGraph NewGraphPt = PGraph::TObj::New();
251 typename PGraph::TObj& NewGraph = *NewGraphPt;
252 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
253 if ((Cmp==1 && EI()>EDat) || (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat)) {
254 if (! NewGraph.IsNode(EI.GetSrcNId())) {
255 NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
257 if (! NewGraph.IsNode(EI.GetDstNId())) {
258 NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
260 NewGraph.AddEdge(EI);
267 template<
class PGraph,
class TEdgeDat>
270 PGraph NewGraphPt = PGraph::TObj::New();
271 typename PGraph::TObj& NewGraph = *NewGraphPt;
272 NewGraph.Reserve(NIdV.
Len(), -1);
273 for (
int n = 0; n < NIdV.
Len(); n++) {
274 NewGraph.AddNode(Graph->GetNI(NIdV[n]));
276 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
277 if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId()) &&
278 ((Cmp==1 && EI()>EDat)|| (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat))) {
279 NewGraph.AddEdge(EI); }
287 template<
class POutGraph,
class PInGraph>
288 POutGraph
ConvertGraph(
const PInGraph& InGraph,
const bool& RenumberNodes) {
289 POutGraph OutGraphPt = POutGraph::TObj::New();
290 typename POutGraph::TObj& OutGraph = *OutGraphPt;
291 OutGraph.Reserve(InGraph->GetNodes(), InGraph->GetEdges());
292 if (! RenumberNodes) {
293 for (
typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
294 OutGraph.AddNode(NI.GetId());
296 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
297 OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
299 OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId()); }
302 TIntSet NIdSet(InGraph->GetNodes());
303 for (
typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
304 const int nid = NIdSet.
AddKey(NI.GetId());
305 OutGraph.AddNode(nid);
307 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
308 const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
309 const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
310 OutGraph.AddEdge(SrcNId, DstNId);
312 OutGraph.AddEdge(DstNId, SrcNId); }
319 namespace TSnapDetail {
321 template <
class POutGraph,
class PInGraph,
bool IsMultiGraph>
323 static POutGraph
Do(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes) {
324 POutGraph OutGraphPt = POutGraph::TObj::New();
325 typename POutGraph::TObj& OutGraph = *OutGraphPt;
326 if (! RenumberNodes) {
327 for (
int n = 0; n < NIdV.
Len(); n++) {
328 OutGraph.AddNode(NIdV[n]);
330 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
331 if (! OutGraph.IsNode(EI.GetSrcNId()) || ! OutGraph.IsNode(EI.GetDstNId())) {
continue; }
332 OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
334 OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId());
338 TIntSet NIdSet(InGraph->GetNodes());
339 for (
int n = 0; n < NIdV.
Len(); n++) {
340 const int NId = NIdSet.
AddKey(NIdV[n]);
341 OutGraph.AddNode(NId);
343 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
344 const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
345 const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
346 if (! OutGraph.IsNode(SrcNId) || ! OutGraph.IsNode(DstNId)) {
continue; }
347 OutGraph.AddEdge(SrcNId, DstNId);
349 OutGraph.AddEdge(DstNId, SrcNId);
358 template <
class POutGraph,
class PInGraph>
360 static POutGraph
Do(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes) {
361 POutGraph OutGraphPt = POutGraph::TObj::New();
362 typename POutGraph::TObj& OutGraph = *OutGraphPt;
363 if (! RenumberNodes) {
364 for (
int n = 0; n < NIdV.
Len(); n++) {
365 OutGraph.AddNode(NIdV[n]); }
366 for (
int n = 0; n < NIdV.
Len(); n++) {
367 typename PInGraph::TObj::TNodeI NI = InGraph->GetNI(NIdV[n]);
368 for (
int e = 0; e < NI.GetOutDeg(); e++) {
369 const int dst = NI.GetOutNId(e);
370 if (! OutGraph.IsNode(dst)) {
continue; }
371 OutGraph.AddEdge(NIdV[n], dst);
375 TIntSet NIdSet(InGraph->GetNodes());
376 for (
int n = 0; n < NIdV.
Len(); n++) {
377 const int NId = NIdSet.
AddKey(NIdV[n]);
378 OutGraph.AddNode(NId);
380 for (
int n = 0; n < NIdV.
Len(); n++) {
381 typename PInGraph::TObj::TNodeI NI = InGraph->GetNI(NIdV[n]);
382 const int src = NIdSet.GetKey(NIdV[n]);
383 for (
int e = 0; e < NI.GetOutDeg(); e++) {
384 const int dst = NIdSet.GetKey(NI.GetOutNId(e));
385 if (! OutGraph.IsNode(dst)) {
continue; }
386 OutGraph.AddEdge(src, dst);
397 template<
class POutGraph,
class PInGraph>
402 template<
class POutGraph,
class PInGraph>
405 POutGraph NewGraphPt = POutGraph::TObj::New();
406 typename POutGraph::TObj& NewGraph = *NewGraphPt;
407 NewGraph.Reserve(-1, EIdV.
Len());
408 if (! RenumberNodes) {
410 const int EId = EIdV[
edge];
412 const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
413 const int SrcNId = EI.GetSrcNId();
414 const int DstNId = EI.GetDstNId();
415 if (! NewGraph.IsNode(SrcNId)) {
416 NewGraph.AddNode(SrcNId); }
417 if (! NewGraph.IsNode(DstNId)) {
418 NewGraph.AddNode(DstNId); }
419 NewGraph.AddEdge(SrcNId, DstNId);
423 TIntSet NIdSet(InGraph->GetNodes());
425 const int EId = EIdV[
edge];
427 const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
428 const int SrcNId = NIdSet.AddKey(EI.GetSrcNId());
429 const int DstNId = NIdSet.AddKey(EI.GetDstNId());
430 if (! NewGraph.IsNode(SrcNId)) {
431 NewGraph.AddNode(SrcNId); }
432 if (! NewGraph.IsNode(DstNId)) {
433 NewGraph.AddNode(DstNId); }
434 NewGraph.AddEdge(SrcNId, DstNId);
440 template<
class PGraph>
442 IAssert(NNodes <= Graph->GetNodes());
444 Graph->GetNIdV(NIdV);
446 NIdV.
Del(NNodes, NIdV.
Len()-1);
451 template<
class PGraph>
454 TIntPrV EdgeV(Graph->GetEdges(), 0);
455 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
456 EdgeV.
Add(
TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
459 EdgeV.Del(NEdges, EdgeV.Len()-1);
460 IAssert(EdgeV.Len() == NEdges);
TPair< TInt, TInt > TIntPr
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
TSizeTy Len() const
Returns the number of elements in the vector.
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...
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
POutGraph ConvertSubGraph(const PInGraph &InGraph, const TIntV &NIdV, const bool &RenumberNodes=false)
Returns an induced subgraph of graph InGraph with NIdV nodes with an optional node renumbering...
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
static PGraph Do(const PGraph &Graph, const TIntV &NIdV)
network with data on edges
PGraph GetRndESubGraph(const PGraph &Graph, const int &NEdges)
Returns a random subgraph of graph Graph with NEdges edges.
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...
static POutGraph Do(const PInGraph &InGraph, const TIntV &NIdV, const bool &RenumberNodes)
static PGraph Do(const PGraph &Graph, const TIntV &NIdV)
int AddKey(const TKey &Key)
static POutGraph Do(const PInGraph &InGraph, const TIntV &NIdV, const bool &RenumberNodes)
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
PGraph GetEDatSubGraph(const PGraph &Graph, const TEdgeDat &EDat, const int &Cmp)
Returns a subgraph of graph Graph with edges where edge data matches the parameters.
POutGraph ConvertESubGraph(const PInGraph &InGraph, const TIntV &EIdV, const bool &RenumberNodes=false)
Returns a subgraph of graph InGraph with EIdV edges with an optional node renumbering.
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
PGraph GetESubGraph(const PGraph &Graph, const TIntV &EIdV)
Returns a subgraph of graph Graph with EIdV edges.
PGraph GetRndSubGraph(const PGraph &Graph, const int &NNodes)
Returns an induced random subgraph of graph Graph with NNodes nodes.
network with data on nodes
POutGraph ConvertGraph(const PInGraph &InGraph, const bool &RenumberNodes=false)
Performs conversion of graph InGraph with an optional node renumbering.
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.