SNAP Library, Developer Reference  2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
subgraph.cpp
Go to the documentation of this file.
00001 namespace TSnap {
00002 
00004 // Graph Algorithms
00005 
00006 // RenumberNodes ... Renumber node ids in the subgraph to 0...N-1
00007 PUNGraph GetSubGraph(const PUNGraph& Graph, const TIntV& NIdV, const bool& RenumberNodes) {
00008   //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
00009   PUNGraph NewGraphPt = TUNGraph::New();
00010   TUNGraph& NewGraph = *NewGraphPt;
00011   NewGraph.Reserve(NIdV.Len(), -1);
00012   TIntSet NIdSet(NIdV.Len());
00013   for (int n = 0; n < NIdV.Len(); n++) {
00014     if (Graph->IsNode(NIdV[n])) {
00015       NIdSet.AddKey(NIdV[n]);
00016       if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
00017       else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
00018     }
00019   }
00020   if (! RenumberNodes) {
00021     for (int n = 0; n < NIdSet.Len(); n++) {
00022       const int SrcNId = NIdSet[n];
00023       const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
00024       for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
00025         const int OutNId = NI.GetOutNId(edge);
00026         if (NIdSet.IsKey(OutNId)) {
00027           NewGraph.AddEdge(SrcNId, OutNId); }
00028       }
00029     }
00030   } else {
00031     for (int n = 0; n < NIdSet.Len(); n++) {
00032       const int SrcNId = NIdSet[n];
00033       const TUNGraph::TNodeI NI = Graph->GetNI(SrcNId);
00034       for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
00035         const int OutNId = NI.GetOutNId(edge);
00036         if (NIdSet.IsKey(OutNId)) {
00037           NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
00038       }
00039     }
00040   }
00041   return NewGraphPt;
00042 }
00043 
00044 // RenumberNodes ... Renumber node ids in the subgraph to 0...N-1
00045 PNGraph GetSubGraph(const PNGraph& Graph, const TIntV& NIdV, const bool& RenumberNodes) {
00046   //if (! RenumberNodes) { return TSnap::GetSubGraph(Graph, NIdV); }
00047   PNGraph NewGraphPt = TNGraph::New();
00048   TNGraph& NewGraph = *NewGraphPt;
00049   NewGraph.Reserve(NIdV.Len(), -1);
00050   TIntSet NIdSet(NIdV.Len());
00051   for (int n = 0; n < NIdV.Len(); n++) {
00052     if (Graph->IsNode(NIdV[n])) {
00053       NIdSet.AddKey(NIdV[n]);
00054       if (! RenumberNodes) { NewGraph.AddNode(NIdV[n]); }
00055       else { NewGraph.AddNode(NIdSet.GetKeyId(NIdV[n])); }
00056     }
00057   }
00058   if (! RenumberNodes) {
00059     for (int n = 0; n < NIdSet.Len(); n++) {
00060       const int SrcNId = NIdSet[n];
00061       const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
00062       for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
00063         const int OutNId = NI.GetOutNId(edge);
00064         if (NIdSet.IsKey(OutNId)) {
00065           NewGraph.AddEdge(SrcNId, OutNId); }
00066       }
00067     }
00068   } else {
00069     for (int n = 0; n < NIdSet.Len(); n++) {
00070       const int SrcNId = NIdSet[n];
00071       const TNGraph::TNodeI NI = Graph->GetNI(SrcNId);
00072       for (int edge = 0; edge < NI.GetOutDeg(); edge++) {
00073         const int OutNId = NI.GetOutNId(edge);
00074         if (NIdSet.IsKey(OutNId)) {
00075           NewGraph.AddEdge(NIdSet.GetKeyId(SrcNId), NIdSet.GetKeyId(OutNId)); }
00076       }
00077     }
00078   }
00079   return NewGraphPt;
00080 }
00081 
00082 
00083 } // namespace TSnap