18 template<
class PGraph> PGraph
GetSubGraph(
const PGraph& Graph,
const TIntV& NIdV);
52 template<
class PGraph> PGraph
GetESubGraph(
const PGraph& Graph,
const TIntV& EIdV);
71 template<
class PGraph,
class TEdgeDat> PGraph
GetEDatSubGraph(
const PGraph& Graph,
const TEdgeDat& EDat,
const int&
Cmp);
87 template<
class PGraph,
class TEdgeDat> PGraph
GetEDatSubGraph(
const PGraph& Graph,
const TIntV& NIdV,
const TEdgeDat& EDat,
const int&
Cmp);
102 template<
class POutGraph,
class PInGraph> POutGraph
ConvertGraph(
const PInGraph& InGraph,
const bool& RenumberNodes=
false);
115 template<
class POutGraph,
class PInGraph> POutGraph
ConvertSubGraph(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes=
false);
129 template<
class POutGraph,
class PInGraph> POutGraph
ConvertESubGraph(
const PInGraph& InGraph,
const TIntV& EIdV,
const bool& RenumberNodes=
false);
137 template<
class PGraph> PGraph
GetRndSubGraph(
const PGraph& Graph,
const int& NNodes);
142 template<
class PGraph> PGraph
GetRndESubGraph(
const PGraph& Graph,
const int& NEdges);
152 namespace TSnapDetail {
154 template <
class PGraph,
bool IsMultiGraph>
156 static PGraph
Do(
const PGraph& Graph,
const TIntV& NIdV) {
157 PGraph NewGraphPt = PGraph::TObj::New();
158 typename PGraph::TObj& NewGraph = *NewGraphPt;
159 NewGraph.Reserve(NIdV.
Len(), -1);
160 for (
int n = 0; n < NIdV.
Len(); n++) {
161 if (Graph->IsNode(NIdV[n])) {
162 NewGraph.AddNode(Graph->GetNI(NIdV[n])); }
164 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
165 if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId())) {
166 NewGraph.AddEdge(EI); }
173 template <
class PGraph>
175 static PGraph
Do(
const PGraph& Graph,
const TIntV& NIdV) {
177 PGraph NewGraphPt = PGraph::TObj::New();
178 typename PGraph::TObj& NewGraph = *NewGraphPt;
179 NewGraph.Reserve(NIdV.
Len(), -1);
181 for (
int n = 0; n < NIdV.
Len(); n++) {
183 if (Graph->IsNode(NIdV[n])) { NewGraph.AddNode(NIdV[n]); NodeSet.
AddKey(NIdV[n]); } }
185 if (Graph->IsNode(NIdV[n])) { NewGraph.AddNode(Graph->GetNI(NIdV[n])); NodeSet.
AddKey(NIdV[n]); } }
187 for (
int n = 0; n < NodeSet.
Len(); n++) {
188 const int SrcNId = NodeSet[n];
189 const typename PGraph::TObj::TNodeI NI = Graph->GetNI(SrcNId);
191 const int OutNId = NI.GetOutNId(
edge);
192 if (NewGraph.IsNode(OutNId)) {
194 NewGraph.AddEdge(SrcNId, OutNId); }
196 NewGraph.AddEdge(Graph->GetEI(SrcNId, OutNId)); }
206 template<
class PGraph>
212 template<
class PGraph>
217 template<
class PGraph>
220 PGraph NewGraphPt = PGraph::TObj::New();
221 typename PGraph::TObj& NewGraph = *NewGraphPt;
222 NewGraph.Reserve(-1, EIdV.
Len());
224 const int EId = EIdV[
edge];
226 const typename PGraph::TObj::TEdgeI EI = Graph->GetEI(EId);
227 if (! NewGraph.IsNode(EI.GetSrcNId())) {
228 NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
230 if (! NewGraph.IsNode(EI.GetDstNId())) {
231 NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
233 NewGraph.AddEdge(EI);
238 template<
class PGraph>
240 PGraph NewGraphPt = PGraph::TObj::New();
241 typename PGraph::TObj& NewGraph = *NewGraphPt;
242 NewGraph.Reserve(-1, EdgeV.
Len());
245 const int SrcNId = EdgeV[
edge].Val1;
246 const int DstNId = EdgeV[
edge].Val2;
247 if (! NewGraph.IsNode(SrcNId)) {
248 NewGraph.AddNode(Graph->GetNI(SrcNId));
250 if (! NewGraph.IsNode(DstNId)) {
251 NewGraph.AddNode(Graph->GetNI(DstNId));
254 NewGraph.AddEdge(SrcNId, DstNId);
260 template<
class PGraph,
class TEdgeDat>
263 PGraph NewGraphPt = PGraph::TObj::New();
264 typename PGraph::TObj& NewGraph = *NewGraphPt;
265 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
266 if ((Cmp==1 && EI()>EDat) || (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat)) {
267 if (! NewGraph.IsNode(EI.GetSrcNId())) {
268 NewGraph.AddNode(Graph->GetNI(EI.GetSrcNId()));
270 if (! NewGraph.IsNode(EI.GetDstNId())) {
271 NewGraph.AddNode(Graph->GetNI(EI.GetDstNId()));
273 NewGraph.AddEdge(EI);
280 template<
class PGraph,
class TEdgeDat>
283 PGraph NewGraphPt = PGraph::TObj::New();
284 typename PGraph::TObj& NewGraph = *NewGraphPt;
285 NewGraph.Reserve(NIdV.
Len(), -1);
286 for (
int n = 0; n < NIdV.
Len(); n++) {
287 NewGraph.AddNode(Graph->GetNI(NIdV[n]));
289 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
290 if (NewGraph.IsNode(EI.GetSrcNId()) && NewGraph.IsNode(EI.GetDstNId()) &&
291 ((Cmp==1 && EI()>EDat)|| (Cmp==-1 && EI()<EDat) || (Cmp==0 && EI()==EDat))) {
292 NewGraph.AddEdge(EI); }
300 template<
class POutGraph,
class PInGraph>
301 POutGraph
ConvertGraph(
const PInGraph& InGraph,
const bool& RenumberNodes) {
302 POutGraph OutGraphPt = POutGraph::TObj::New();
303 typename POutGraph::TObj& OutGraph = *OutGraphPt;
304 OutGraph.Reserve(InGraph->GetNodes(), InGraph->GetEdges());
305 if (! RenumberNodes) {
306 for (
typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
307 OutGraph.AddNode(NI.GetId());
309 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
310 OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
312 OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId()); }
315 TIntSet NIdSet(InGraph->GetNodes());
316 for (
typename PInGraph::TObj::TNodeI NI = InGraph->BegNI(); NI < InGraph->EndNI(); NI++) {
317 const int nid = NIdSet.
AddKey(NI.GetId());
318 OutGraph.AddNode(nid);
320 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
321 const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
322 const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
323 OutGraph.AddEdge(SrcNId, DstNId);
325 OutGraph.AddEdge(DstNId, SrcNId); }
332 namespace TSnapDetail {
334 template <
class POutGraph,
class PInGraph,
bool IsMultiGraph>
336 static POutGraph
Do(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes) {
337 POutGraph OutGraphPt = POutGraph::TObj::New();
338 typename POutGraph::TObj& OutGraph = *OutGraphPt;
339 if (! RenumberNodes) {
340 for (
int n = 0; n < NIdV.
Len(); n++) {
341 OutGraph.AddNode(NIdV[n]);
343 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
344 if (! OutGraph.IsNode(EI.GetSrcNId()) || ! OutGraph.IsNode(EI.GetDstNId())) {
continue; }
345 OutGraph.AddEdge(EI.GetSrcNId(), EI.GetDstNId());
347 OutGraph.AddEdge(EI.GetDstNId(), EI.GetSrcNId());
351 TIntSet NIdSet(InGraph->GetNodes());
352 for (
int n = 0; n < NIdV.
Len(); n++) {
353 const int NId = NIdSet.
AddKey(NIdV[n]);
354 OutGraph.AddNode(NId);
356 for (
typename PInGraph::TObj::TEdgeI EI = InGraph->BegEI(); EI < InGraph->EndEI(); EI++) {
357 const int SrcNId = NIdSet.GetKeyId(EI.GetSrcNId());
358 const int DstNId = NIdSet.GetKeyId(EI.GetDstNId());
359 if (! OutGraph.IsNode(SrcNId) || ! OutGraph.IsNode(DstNId)) {
continue; }
360 OutGraph.AddEdge(SrcNId, DstNId);
362 OutGraph.AddEdge(DstNId, SrcNId);
371 template <
class POutGraph,
class PInGraph>
373 static POutGraph
Do(
const PInGraph& InGraph,
const TIntV& NIdV,
const bool& RenumberNodes) {
374 POutGraph OutGraphPt = POutGraph::TObj::New();
375 typename POutGraph::TObj& OutGraph = *OutGraphPt;
376 if (! RenumberNodes) {
377 for (
int n = 0; n < NIdV.
Len(); n++) {
378 OutGraph.AddNode(NIdV[n]); }
379 for (
int n = 0; n < NIdV.
Len(); n++) {
380 typename PInGraph::TObj::TNodeI NI = InGraph->GetNI(NIdV[n]);
381 for (
int e = 0; e < NI.GetOutDeg(); e++) {
382 const int dst = NI.GetOutNId(e);
383 if (! OutGraph.IsNode(dst)) {
continue; }
384 OutGraph.AddEdge(NIdV[n], dst);
388 TIntSet NIdSet(InGraph->GetNodes());
389 for (
int n = 0; n < NIdV.
Len(); n++) {
390 const int NId = NIdSet.
AddKey(NIdV[n]);
391 OutGraph.AddNode(NId);
393 for (
int n = 0; n < NIdV.
Len(); n++) {
394 typename PInGraph::TObj::TNodeI NI = InGraph->GetNI(NIdV[n]);
395 const int src = NIdSet.GetKey(NIdV[n]);
396 for (
int e = 0; e < NI.GetOutDeg(); e++) {
397 const int dst = NIdSet.GetKey(NI.GetOutNId(e));
398 if (! OutGraph.IsNode(dst)) {
continue; }
399 OutGraph.AddEdge(src, dst);
410 template<
class POutGraph,
class PInGraph>
415 template<
class POutGraph,
class PInGraph>
418 POutGraph NewGraphPt = POutGraph::TObj::New();
419 typename POutGraph::TObj& NewGraph = *NewGraphPt;
420 NewGraph.Reserve(-1, EIdV.
Len());
421 if (! RenumberNodes) {
423 const int EId = EIdV[
edge];
425 const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
426 const int SrcNId = EI.GetSrcNId();
427 const int DstNId = EI.GetDstNId();
428 if (! NewGraph.IsNode(SrcNId)) {
429 NewGraph.AddNode(SrcNId); }
430 if (! NewGraph.IsNode(DstNId)) {
431 NewGraph.AddNode(DstNId); }
432 NewGraph.AddEdge(SrcNId, DstNId);
436 TIntSet NIdSet(InGraph->GetNodes());
438 const int EId = EIdV[
edge];
440 const typename PInGraph::TObj::TEdgeI EI = InGraph->GetEI(EId);
441 const int SrcNId = NIdSet.AddKey(EI.GetSrcNId());
442 const int DstNId = NIdSet.AddKey(EI.GetDstNId());
443 if (! NewGraph.IsNode(SrcNId)) {
444 NewGraph.AddNode(SrcNId); }
445 if (! NewGraph.IsNode(DstNId)) {
446 NewGraph.AddNode(DstNId); }
447 NewGraph.AddEdge(SrcNId, DstNId);
453 template<
class PGraph>
455 IAssert(NNodes <= Graph->GetNodes());
457 Graph->GetNIdV(NIdV);
459 NIdV.
Del(NNodes, NIdV.
Len()-1);
464 template<
class PGraph>
467 TIntPrV EdgeV(Graph->GetEdges(), 0);
468 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
469 EdgeV.
Add(
TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
472 EdgeV.Del(NEdges, EdgeV.Len()-1);
473 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)
PGraph GetSubGraphRenumber(const PGraph &Graph, const TIntV &NIdV)
Returns an induced subgraph of graph Graph with NIdV nodes with an node renumbering.
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.