SNAP Library 3.0, User Reference  2016-07-20 17:56:49
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
TNetConstraint< PGraph > Class Template Reference

#include <triad.h>

Public Member Functions

 TNetConstraint (const PGraph &GraphPt, const bool &CalcaAll=true)
 
int Len () const
 
double GetC (const int &ConstraintN) const
 
TIntPr GetNodePr (const int &ConstraintN) const
 
double GetEdgeC (const int &NId1, const int &NId2) const
 
double GetNodeC (const int &NId) const
 
void AddConstraint (const int &NId1, const int &NId2)
 
void CalcConstraints ()
 
void CalcConstraints (const int &NId)
 
void Dump () const
 

Static Public Member Functions

static void Test ()
 

Public Attributes

PGraph Graph
 
THash< TIntPr, TFltNodePrCH
 

Detailed Description

template<class PGraph>
class TNetConstraint< PGraph >

Definition at line 869 of file triad.h.

Constructor & Destructor Documentation

template<class PGraph >
TNetConstraint< PGraph >::TNetConstraint ( const PGraph &  GraphPt,
const bool &  CalcaAll = true 
)

Definition at line 888 of file triad.h.

888  : Graph(GraphPt) {
889  CAssert(! HasGraphFlag(typename PGraph::TObj, gfMultiGraph)); // must not be multigraph
890  if (CalcaAll) {
891  CalcConstraints();
892  }
893 }
void CalcConstraints()
Definition: triad.h:948
PGraph Graph
Definition: triad.h:871
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
Definition: gbase.h:14
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
#define CAssert(Cond)
Definition: bd.h:302

Member Function Documentation

template<class PGraph >
void TNetConstraint< PGraph >::AddConstraint ( const int &  NId1,
const int &  NId2 
)

Definition at line 924 of file triad.h.

924  {
925  if (NId1==NId2 || NodePrCH.IsKey(TIntPr(NId1, NId2))) {
926  return;
927  }
928  typename PGraph::TObj::TNodeI NI1 = Graph->GetNI(NId1);
929  double Constraint = 0.0;
930  if (NI1.IsOutNId(NId2)) { // is direct edge
931  Constraint += 1.0/(double) NI1.GetOutDeg();
932  }
933  const double SrcC = 1.0/(double) NI1.GetOutDeg();
934  for (int e = 0; e < NI1.GetOutDeg(); e++) {
935  const int MidNId = NI1.GetOutNId(e);
936  if (MidNId == NId1 || MidNId == NId2) { continue; }
937  const typename PGraph::TObj::TNodeI MidNI = Graph->GetNI(MidNId);
938  if (MidNI.IsOutNId(NId2)) {
939  Constraint += SrcC * (1.0/(double)MidNI.GetOutDeg());
940  }
941  }
942  if (Constraint==0) { return; }
943  Constraint = TMath::Sqr(Constraint);
944  NodePrCH.AddDat(TIntPr(NId1, NId2), Constraint);
945 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
PGraph Graph
Definition: triad.h:871
static double Sqr(const double &x)
Definition: xmath.h:12
Definition: ds.h:32
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class PGraph >
void TNetConstraint< PGraph >::CalcConstraints ( )

Definition at line 948 of file triad.h.

948  {
949  // add edges
950  for (typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
951  AddConstraint(EI.GetSrcNId(), EI.GetDstNId());
952  AddConstraint(EI.GetDstNId(), EI.GetSrcNId());
953  }
954  // add open triads
955  for (typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
956  for (int i = 0; i < NI.GetDeg(); i++) {
957  const int NId1 = NI.GetNbrNId(i);
958  for (int j = 0; j < NI.GetDeg(); j++) {
959  const int NId2 = NI.GetNbrNId(j);
960  AddConstraint(NId1, NId2);
961  }
962  }
963  }
965 }
PGraph Graph
Definition: triad.h:871
void AddConstraint(const int &NId1, const int &NId2)
Definition: triad.h:924
void SortByKey(const bool &Asc=true)
Definition: hash.h:249
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
template<class PGraph >
void TNetConstraint< PGraph >::CalcConstraints ( const int &  NId)

Definition at line 969 of file triad.h.

969  {
970  typename PGraph::TObj::TNodeI StartNI = Graph->GetNI(NId);
971  TIntSet SeenSet;
972  for (int e = 0; e < StartNI.GetOutDeg(); e++) {
973  typename PGraph::TObj::TNodeI MidNI = Graph->GetNI(StartNI.GetOutNId(e));
974  AddConstraint(NId, MidNI.GetId());
975  for (int i = 0; i < MidNI.GetOutDeg(); i++) {
976  const int EndNId = MidNI.GetOutNId(i);
977  if (! SeenSet.IsKey(EndNId)) {
978  AddConstraint(NId, EndNId);
979  SeenSet.AddKey(EndNId);
980  }
981  }
982  }
983 }
PGraph Graph
Definition: triad.h:871
bool IsKey(const TKey &Key) const
Definition: shash.h:1148
void AddConstraint(const int &NId1, const int &NId2)
Definition: triad.h:924
int AddKey(const TKey &Key)
Definition: shash.h:1254
template<class PGraph >
void TNetConstraint< PGraph >::Dump ( ) const

Definition at line 986 of file triad.h.

986  {
987  printf("Edge network constraint: (%d, %d)\n", Graph->GetNodes(), Graph->GetEdges());
988  for (int e = 0; e < NodePrCH.Len(); e++) {
989  printf(" %4d %4d : %f\n", NodePrCH.GetKey(e).Val1(), NodePrCH.GetKey(e).Val2(), NodePrCH[e].Val);
990  }
991  printf("\n");
992 }
PGraph Graph
Definition: triad.h:871
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
int Len() const
Definition: hash.h:186
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph>
double TNetConstraint< PGraph >::GetC ( const int &  ConstraintN) const
inline

Definition at line 876 of file triad.h.

876 { return NodePrCH[ConstraintN]; }
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
template<class PGraph >
double TNetConstraint< PGraph >::GetEdgeC ( const int &  NId1,
const int &  NId2 
) const

Definition at line 896 of file triad.h.

896  {
897  if (NodePrCH.IsKey(TIntPr(NId1, NId2))) {
898  return NodePrCH.GetDat(TIntPr(NId1, NId2)); }
899  else {
900  return 0.0; }
901 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
Definition: ds.h:32
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
bool IsKey(const TKey &Key) const
Definition: hash.h:216
template<class PGraph >
double TNetConstraint< PGraph >::GetNodeC ( const int &  NId) const

Definition at line 904 of file triad.h.

904  {
905  typename PGraph::TObj::TNodeI NI1 = Graph->GetNI(NId);
906  if (NI1.GetOutDeg() == 0) { return 0.0; }
907  int KeyId = -1;
908  for (int k = 0; k<NI1.GetOutDeg(); k++) {
909  KeyId = NodePrCH.GetKeyId(TIntPr(NI1.GetId(), NI1.GetOutNId(k)));
910  if (KeyId > -1) { break; }
911  }
912  if (KeyId < 0) { return 0.0; }
913  double Constraint = NodePrCH[KeyId];
914  for (int i = KeyId-1; i >-1 && NodePrCH.GetKey(i).Val1()==NId; i--) {
915  Constraint += NodePrCH[i];
916  }
917  for (int i = KeyId+1; i < NodePrCH.Len() && NodePrCH.GetKey(i).Val1()==NId; i++) {
918  Constraint += NodePrCH[i];
919  }
920  return Constraint;
921 }
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
PGraph Graph
Definition: triad.h:871
int GetKeyId(const TKey &Key) const
Definition: hash.h:424
TVal1 Val1
Definition: ds.h:34
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
int Len() const
Definition: hash.h:186
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph>
TIntPr TNetConstraint< PGraph >::GetNodePr ( const int &  ConstraintN) const
inline

Definition at line 877 of file triad.h.

877 { return NodePrCH.GetKey(ConstraintN); }
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class PGraph>
int TNetConstraint< PGraph >::Len ( ) const
inline

Definition at line 875 of file triad.h.

875 { return NodePrCH.Len(); }
THash< TIntPr, TFlt > NodePrCH
Definition: triad.h:872
int Len() const
Definition: hash.h:186
template<class PGraph >
void TNetConstraint< PGraph >::Test ( )
static

Definition at line 997 of file triad.h.

997  {
998  PUNGraph G = TUNGraph::New();
999  G->AddNode(0); G->AddNode(1); G->AddNode(2); G->AddNode(3);
1000  G->AddNode(4); G->AddNode(5); G->AddNode(6);
1001  G->AddEdge(0,1); G->AddEdge(0,2); G->AddEdge(0,3); G->AddEdge(0,4); G->AddEdge(0,5); G->AddEdge(0,6);
1002  G->AddEdge(1,2); G->AddEdge(1,5); G->AddEdge(1,6);
1003  G->AddEdge(2,4);
1004  TNetConstraint<PUNGraph> NetConstraint(G, true);
1005  // NetConstraint.CalcConstraints(0);
1006  NetConstraint.Dump();
1007  printf("middle node network constraint: %f\n", NetConstraint.GetNodeC(0));
1008 }
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
static PUNGraph New()
Static constructor that returns a pointer to the graph. Call: PUNGraph Graph = TUNGraph::New().
Definition: graph.h:155
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92

Member Data Documentation

template<class PGraph>
PGraph TNetConstraint< PGraph >::Graph

Definition at line 871 of file triad.h.

template<class PGraph>
THash<TIntPr, TFlt> TNetConstraint< PGraph >::NodePrCH

Definition at line 872 of file triad.h.


The documentation for this class was generated from the following file: