10 template <
class TNodeData,
bool IsDir>
39 TNode(
const int& InVecId,
const int& OutVecId,
const TNodeDat& NodeDat) :
121 static void AddSorted(
int* Beg,
int* End,
const int& Val);
122 static const int*
BinSearch(
const int* Beg,
const int* End,
const int& Val);
123 static const int*
BinSearch2(
const int* Beg,
const int* End,
const int& Val);
135 TBigNet(
const int& Nodes,
const TSize& Edges,
const bool& Sources=
false);
154 int AddNode(
int NId,
const int& InDeg,
const int& OutDeg);
155 int AddNode(
int NId,
const int& InDeg,
const int& OutDeg,
const TNodeDat& NodeDat);
175 TEdgeI
GetEI(
const int& EId)
const;
186 int AddEdge(
const int& SrcNId,
const int& DstNId);
187 bool IsEdge(
const int& SrcNId,
const int& DstNId,
const bool& Dir=
true)
const;
206 void Defrag(
const bool& OnlyNodeLinks =
false) { }
212 static void SaveToDisk(
const TStr& InFNm,
const TStr& OutFNm,
const bool& SaveSparseHash);
220 template <
class TNodeData,
bool IsDir>
struct IsNodeDat<
TBigNet<TNodeData, IsDir> > {
enum {
Val = 1 }; };
223 template <
class TNodeData,
bool IsDir>
225 if (
NodeHI.IsEnd())
return;
237 template <
class TNodeData,
bool IsDir>
239 printf(
"NodeId: %d\n", GetId());
240 printf(
" I:%4d]", GetInDeg());
241 for (
int i = 0; i < GetInDeg(); i++) { printf(
" %d", GetInNId(i)); }
242 printf(
"\n O:%4d]", GetOutDeg());
243 for (
int i = 0; i < GetOutDeg(); i++) { printf(
" %d", GetOutNId(i)); }
247 template <
class TNodeData,
bool IsDir>
252 int Half, Len = int(End-Beg);
256 if (*Mid < Val) { Beg=Mid+1; Len=Len-Half-1; }
else { Len=Half; } }
257 IAssertR(*Beg != Val,
"Value already existis");
259 memmove(Beg+1, Beg,
sizeof(
int)*(End-Beg-1));
263 template <
class TNodeData,
bool IsDir>
265 End--;
const int *Mid;
266 while (Beg <= End) { Mid = Beg+(End-Beg)/2;
267 if (*Mid == Val) {
return Mid; }
268 else if (Val < *Mid) { End=Mid-1; }
else { Beg=Mid+1; }
273 template <
class TNodeData,
bool IsDir>
275 const int* First = Beg;
276 const int* Last = End;
278 TSize len = End-Beg, half;
280 half = len>>1; Mid=First+half;
281 if (*Mid < Val) { First = Mid; First++; len=len-half-1; }
284 return First==Last ? Last-1 : First;
287 template <
class TNodeData,
bool IsDir>
295 IAssertR(! IsDir,
"Jure: not clear what happens is graph is Undirected and has only sources.");
300 template <
class TNodeData,
bool IsDir>
309 template <
class TNodeData,
bool IsDir>
311 for (
int i = 1; i <int(
gfMx); i++) {
313 else { printf(
" -"); }
319 template <
class TNodeData,
bool IsDir>
322 if (NId == -1) { NId = MxNId; MxNId.Val++; }
323 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
324 TNode& Node = NodeH.AddDat(NId);
326 Node.
InVId = Pool.AddEmptyV(InDeg);
327 Node.
OutVId = Pool.AddEmptyV(OutDeg);
331 template <
class TNodeData,
bool IsDir>
334 if (NId == -1) { NId = MxNId; MxNId.Val++; }
335 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
336 TNode& Node = NodeH.AddDat(NId);
338 Node.
InVId = Pool.AddEmptyV(InDeg);
339 Node.
OutVId = Pool.AddEmptyV(OutDeg);
344 template <
class TNodeData,
bool IsDir>
347 if (NId == -1) { NId = MxNId; MxNId.Val++; }
348 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
349 TNode& Node = NodeH.AddDat(NId);
351 Node.
InVId = Pool.AddEmptyV(Deg);
356 template <
class TNodeData,
bool IsDir>
359 if (NId == -1) { NId = MxNId; MxNId.Val++; }
360 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
361 TNode& Node = NodeH.AddDat(NId);
363 Node.
InVId = Pool.AddEmptyV(Deg);
369 template <
class TNodeData,
bool IsDir>
373 if (NId == -1) { NId = MxNId; MxNId.Val++; }
374 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
375 TNode& Node = NodeH.AddDat(NId);
377 Node.
InVId = Pool.AddV(InNIdV);
378 Node.
OutVId = Pool.AddV(OutNIdV);
382 template <
class TNodeData,
bool IsDir>
386 if (NId == -1) { NId = MxNId; MxNId.Val++; }
387 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
388 TNode& Node = NodeH.AddDat(NId);
390 Node.
InVId = Pool.AddV(InNIdV);
391 Node.
OutVId = Pool.AddV(OutNIdV);
396 template <
class TNodeData,
bool IsDir>
400 if (NId == -1) { NId = MxNId; MxNId.Val++; }
401 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
402 TNode& Node = NodeH.AddDat(NId);
404 Node.
InVId = Pool.AddV(EdgeNIdV);
409 template <
class TNodeData,
bool IsDir>
413 if (NId == -1) { NId = MxNId; MxNId.Val++; }
414 else { MxNId =
TMath::Mx(NId+1, MxNId()); }
415 TNode& Node = NodeH.AddDat(NId);
417 Node.
InVId = Pool.AddV(EdgeNIdV);
423 template <
class TNodeData,
bool IsDir>
431 template <
class TNodeData,
bool IsDir>
439 template <
class TNodeData,
bool IsDir>
442 Pool.GetV(Node.
InVId, InNIdV);
445 template <
class TNodeData,
bool IsDir>
448 Pool.GetV(Node.
OutVId, OutNIdV);
453 template <
class TNodeData,
bool IsDir>
458 GetOutNIdV(NId, OutV);
459 for (
int i = 0; i < OutV.
Len(); i++) {
460 if (OutV[i] == DelNId) {
break; }
462 const TNode& N = NodeH.GetDat(OutV[i]);
463 int *InNIdV = (
int *) Pool.GetValVPt(N.
InVId);
464 const int Deg = Pool.GetVLen(N.
InVId);
465 int* Val = (
int *) BinSearch(InNIdV, InNIdV+Deg, NId);
467 printf(
"BAD: Can't find: OUT: NId: %d -- OutNbrId: %d\n", NId, OutV[i]());
471 memcpy(Val, Val+1,
sizeof(
int)*
int(InNIdV+Deg-Val));
472 *(InNIdV+Deg-1) = DelNId; NDel++;
479 for (
int i = 0; i < InV.
Len(); i++) {
480 if (InV[i] == DelNId) {
break; }
482 const TNode& N = NodeH.GetDat(InV[i]);
483 int *OutNIdV = (
int *) Pool.GetValVPt(N.
OutVId);
484 const int Deg = Pool.GetVLen(N.
OutVId);
485 int* Val = (
int *) BinSearch(OutNIdV, OutNIdV+Deg, NId);
487 printf(
"IN: NId: %d -- InNbrId: %d\n", NId, OutV[i]());
491 memcpy(Val, Val+1,
sizeof(
int)*
int(OutNIdV+Deg-Val));
492 *(OutNIdV+Deg-1) = DelNId; NDel++;
500 template <
class TNodeData,
bool IsDir>
502 const int DelEdges = IsolateNode(NId);
507 template <
class TNodeData,
bool IsDir>
509 if (NId == DelNId) {
return true; }
511 GetOutNIdV(NId, OutV);
512 if (OutV.
Empty() || OutV[0] == DelNId) {
return true; }
517 template <
class TNodeData,
bool IsDir>
521 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
522 const int NId = NI.GetId();
523 GetOutNIdV(NId, OutV);
524 for (
int i = 0; i < OutV.
Len(); i++) {
525 if (OutV[i] == DelNId) { DelEdges++; }
531 template <
class TNodeData,
bool IsDir>
533 Pool.CompactPool(DelNId);
536 template <
class TNodeData,
bool IsDir>
539 int* OutV = (
int *)Pool.GetValVPt(Src.
OutVId);
540 const int OutVLen = Pool.GetVLen(Src.
OutVId);
541 Assert(BinSearch(OutV, OutV+OutVLen, DstNId)==NULL);
542 AddSorted(OutV, OutV+OutVLen, DstNId);
543 if (! OnlySources()) {
545 int* InV = (
int *)Pool.GetValVPt(Dst.
InVId);
546 const int InVLen = Pool.GetVLen(Dst.
InVId);
547 AddSorted(InV, InV+InVLen, SrcNId);
552 template <
class TNodeData,
bool IsDir>
555 if (! IsNode(SrcNId, Src)) {
return false; }
556 int* OutV1 = (
int *)Pool.GetValVPt(Src.
OutVId);
557 const bool IsEdge = BinSearch(OutV1, OutV1+Pool.GetVLen(Src.
OutVId), DstNId) != NULL;
558 if (IsEdge && OnlySources()) {
return true; }
559 const bool IsDstNode = IsNode(DstNId, Dst);
560 if (! IsDstNode) {
return false; }
561 else if (IsEdge) {
return true; }
563 int* OutV2 = (
int *)Pool.GetValVPt(Dst.
OutVId);
564 return BinSearch(OutV2, OutV2+Pool.GetVLen(Dst.
OutVId), SrcNId) != NULL; }
565 else {
return false; }
568 template <
class TNodeData,
bool IsDir>
575 template <
class TNodeData,
bool IsDir>
577 printf(
"Sorting Edges... ");
581 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
582 const int NId = NI.GetId();
583 GetOutNIdV(NId, OutV);
589 if (++cnt %
Mega(1) == 0) { printf(
"\r sort:%dm %s", cnt/
Mega(1), ExeTm.
GetStr()); }
591 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
592 const int NId = NI.GetId();
593 GetOutNIdV(NId, OutV);
597 if (++cnt %
Mega(1) == 0) { printf(
"\r check sorted:%dm %s", cnt/
Mega(1), ExeTm.
GetStr()); }
599 printf(
"done. [%s]\n", ExeTm.
GetStr());
603 template <
class TNodeData,
bool IsDir>
607 if (ExpectNodes == 0) ExpectNodes=4*GetNodes();
608 printf(
"\nInverting BigNet: reserved for %s nodes\n",
TInt::GetMegaStr(ExpectNodes).CStr());
611 TDegHash InDegH(ExpectNodes);
615 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, c++) {
616 for (
int e = 0; e < NI.GetOutDeg(); e++) {
617 InDegH.AddDat(NI.GetOutNId(e)) += 1; }
620 printf(
"\n Resizing NodePool: %lld -> %lld\n",
uint64(Pool.Reserved()),
uint64(GetEdges()));
621 if (2*GetEdges() > Pool.Reserved()) {
622 Pool.Reserve(2*GetEdges()); }
624 printf(
"NodeH: %s nodes, InDeg: %s nodes, Reserve: %s\n",
TInt::GetMegaStr(NodeH.Len()).CStr(),
626 NodeH.Reserve(ExpectNodes);
627 for (TDegHash::TIter I = InDegH.BegI(); I < InDegH.EndI(); I++) {
628 const int NId = I->Key, InDeg = I->Dat;
630 AddNode(NId, InDeg, 0); NDest++; }
632 TNode& Node = GetNode(NId);
634 Node.
InVId = Pool.AddEmptyV(InDeg); }
637 printf(
"Added: %lld destination nodes\n",
uint64(NDest));
638 printf(
"Graph nodes: %lld nodes\n",
uint64(GetNodes()));
641 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, c++)
642 NIdToPtH.
AddDat(NI.GetId(), (
int *)Pool.GetValVPt(NI.GetInVId()));
644 printf(
"Adding edges...\n");
646 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, c++) {
647 const int SrcNId = NI.GetId();
648 for (
int e = 0; e < NI.GetOutDeg(); e++) {
649 TIntPt& InV = NIdToPtH.
GetDat(NI.GetOutNId(e));
651 *InV = SrcNId; InV++;
656 printf(
"\nSorting in-links...\n");
658 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, c++) {
659 Pool.
GetV(NI.GetInVId(), InNIdV);
663 printf(
"\r...done [%g]\n", ExeTm.
GetSecs());
667 template <
class TNodeData,
bool IsDir>
669 uint64 NDup=0, NResolve=0, NAddit=0, cnt = 0;
671 IAssertR(! IsDir,
"Only undirected networks are supported");
672 printf(
"Rewiring the network... 1");
673 Pool.ShuffleAll(Rnd); printf(
"[%s]\n", ExeTm.
GetStr());
675 printf(
" sorting edges...\n");
677 printf(
" done [%s]\n", ExeTm.
GetStr());
679 printf(
" removing duplicates...\n");
680 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, cnt++) {
681 const int VId = NI.GetOutVId();
682 int Len = Pool.GetVLen(VId);
683 int* V = (
int *)Pool.GetValVPt(VId);
684 for (
int i = 0; i < Len-1 && V[i]!=DelNId; i++) {
685 if (V[i] == V[i+1] || V[i]==NI.GetId()) {
686 memcpy(V+i, V+i+1,
sizeof(
int)*(Len-i-1)); i--;
687 V[Len-1] = DelNId; NDup++;
690 if (Len > 0 && V[Len-1]==NI.GetId()) { V[Len-1] = DelNId; NDup++; }
691 if (cnt %
Mega(10) == 0) { printf(
"."); fflush(stdout); }
693 printf(
"\n %llu duplicate edges removed [%s]\n", NDup, ExeTm.
GetStr());
694 if (OnlySources()) {
return; }
696 printf(
" resolving one way edges...\n"); cnt=0; fflush(stdout);
697 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, cnt++) {
698 for (
int e = 0; e < NI.GetOutDeg(); e++) {
699 if (NI.GetOutNId(e) == DelNId) {
continue; }
700 const int InVId = GetNode(NI.GetOutNId(e)).InVId;
701 const int InVLen = Pool.GetVLen(InVId);
IAssert(InVLen>=0 && InVLen <
Mega(3));
702 int* InV = (
int *) Pool.GetValVPt(InVId);
703 int* Val = (
int *) BinSearch2(InV, InV+InVLen, NI.GetId());
704 if (*Val == NI.GetId()) {
continue; }
705 if (InVLen>0 && InV[InVLen-1] == DelNId) {
706 IAssert((InVLen-(Val-InV)-1) >= 0);
707 memmove(Val+1, Val,
sizeof(
int)*(InVLen-(Val-InV)-1));
710 memmove(NI.OutNIdV+e, NI.OutNIdV+e+1,
sizeof(
int)*(NI.GetOutDeg()-e-1));
711 NI.OutNIdV[NI.GetOutDeg()-1]=DelNId; e--;
715 if (cnt %
Mega(10) == 0) { printf(
"."); fflush(stdout); }
717 printf(
"\n %llu resolved edges [%s]\n", NResolve, ExeTm.
GetStr());
719 printf(
" filling empty edge slots...\n");
723 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
724 if (NI.GetOutNId(NI.GetOutDeg()-1) == DelNId) {
726 for (
int s = NI.GetOutDeg()-1; s >= 0; s--) {
727 if (NI.GetOutNId(s) == DelNId) { NSlots++; }
730 SlotNIdV.
Add(
TIntPr(NI.GetId(), NSlots));
731 SlotNIdH.
AddDat(NI.GetId(), NSlots);
735 printf(
" %d nodes, with %d spokes available, %d edges\n", SlotNIdH.
Len(), CumSlots, CumSlots/2);
737 for (
int ni1 = 0; ni1 < NIdV.Len(); ni1++) {
738 const int nid = NIdV[ni1];
739 if (! SlotNIdH.
IsKey(nid) || SlotNIdH.
GetDat(nid) == 0) {
continue; }
740 const int NI1Slots = SlotNIdH.
GetDat(nid);
743 for (
int NTries = 0; NTries < 4*NI1Slots && NI.
GetOutNId(NI.
GetOutDeg()-1) == DelNId; NTries++) {
745 if (nid == nid2) {
continue; }
751 if (*V1 != DelNId) { memmove(V1+1, V1,
sizeof(
int)*(NI.
GetOutDeg()-(V1-NI.
OutNIdV)-1)); }
752 if (*V2 != DelNId) { memmove(V2+1, V2,
sizeof(
int)*(NI2.
GetInDeg()-(V2-NI2.
InNIdV)-1)); }
757 if (SlotNIdH.
GetDat(nid2) == 0) { SlotNIdH.
DelKey(nid2);
continue; }
758 if (SlotNIdH.
GetDat(nid) == 0) { SlotNIdH.
DelKey(nid);
break; }
760 if (ni1 %
Mega(1) == 0) { printf(
"."); fflush(stdout); }
762 printf(
" %llu edges added.\nDONE. TOTAL REWIRE TIME [%s]\n\n", NAddit, ExeTm.
GetStr());
765 template <
class TNodeData,
bool IsDir>
767 IAssert(RenumberNodes ==
false);
770 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
771 Graph->
AddNode(NI.GetId(), Pool, NI.GetInVId(), NI.GetOutVId());
776 template <
class TNodeData,
bool IsDir>
780 for (
int i = 0; i < NIdV.
Len(); i++) {
783 for (
int i = 0; i < NIdV.
Len(); i++) {
784 const int SrcNId = NIdV[i];
785 const TNodeI NI = GetNI(SrcNId);
786 int InDeg=0, OutDeg=0;
793 for (
int i = 0; i < NIdV.
Len(); i++) {
794 const int SrcNId = NIdV[i];
795 const TNodeI NI = GetNI(SrcNId);
796 for (
int e = 0; e < NI.
GetOutDeg(); e++) {
798 if (Graph->
IsNode(DstNId)) {
799 Graph->
AddEdge(SrcNId, DstNId); }
805 template <
class TNodeData,
bool IsDir>
807 const bool isDir = IsDir, onlySources = HasFlag(
gfSources);
811 for (
int i = 0; i < NIdV.
Len(); i++) { NIdDegH.
AddDat(NIdV[i]); }
812 for (
int i = 0; i < NIdV.
Len(); i++) {
813 const TNodeI NI = GetNI(NIdV[i]);
814 int InDeg=0, OutDeg=0;
815 for (
int n = 0; n < NI.
GetOutDeg(); n++) {
816 if (NIdDegH.IsKey(NI.
GetOutNId(n))) OutDeg++; }
817 if (! onlySources && isDir) {
818 for (
int n = 0; n < NI.
GetInDeg(); n++) {
819 if (NIdDegH.IsKey(NI.
GetInNId(n))) InDeg++; }
822 NIdDegH.GetDat(NIdV[i]) =
TIntPr(InDeg, OutDeg);
827 TBNet& NewNet = *NewNetPt;
828 NewNet.Flags = Flags;
830 if (isDir || onlySources) {
831 for (
int i = 0; i < NIdV.
Len(); i++) {
832 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
833 NewNet.AddNode(NIdV[i], Deg.
Val1, Deg.
Val2, GetNDat(NIdV[i])); }
835 for (
int i = 0; i < NIdV.
Len(); i++) {
836 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
837 NewNet.AddUndirNode(NIdV[i], Deg.
Val2, GetNDat(NIdV[i])); }
840 for (
int i = 0; i < NIdV.
Len(); i++) {
842 const TNodeI NI = GetNI(NId);
843 int *NIdVPt = (
int *) NewNet.GetOutNIdVPt(NId);
845 for (
int e = 0; e < NI.
GetOutDeg(); e++) {
847 if (NewNet.IsNode(Dst)) {
848 *NIdVPt = Dst; NIdVPt++; deg++; }
850 EAssert(deg == NIdDegH.GetDat(NId).Val2);
851 if (isDir && ! onlySources) {
854 int * NIdVPt = (
int *) NewNet.GetInNIdVPt(NId);
856 for (
int e = 0; e < NI.
GetInDeg(); e++) {
858 if (NewNet.IsNode(Dst)) {
859 *NIdVPt = Dst; NIdVPt++; deg++; }
861 EAssert(deg == NIdDegH.GetDat(NId).Val1);
867 template <
class TNodeData,
bool IsDir>
869 printf(
"Set subgraph on %d nodes\n", NIdV.
Len());
TExeTm ExeTm;
870 const bool isDir = IsDir, onlySources = HasFlag(
gfSources);
874 for (
int i = 0; i < NIdV.
Len(); i++) { NIdDegH.
AddDat(NIdV[i]); }
875 for (
int i = 0; i < NIdV.
Len(); i++) {
876 const TNodeI NI = GetNI(NIdV[i]);
877 int InDeg=0, OutDeg=0;
878 for (
int n = 0; n < NI.
GetOutDeg(); n++) {
879 if (NIdDegH.IsKey(NI.
GetOutNId(n))) OutDeg++; }
880 if (! onlySources && isDir) {
881 for (
int n = 0; n < NI.
GetInDeg(); n++) {
882 if (NIdDegH.IsKey(NI.
GetInNId(n))) InDeg++; }
885 NIdDegH.GetDat(NIdV[i]) =
TIntPr(InDeg, OutDeg);
892 NewNet.
Flags = Flags;
895 if (! RenumberNodes) {
896 if (isDir || onlySources) {
897 for (
int i = 0; i < NIdV.
Len(); i++) {
898 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
901 for (
int i = 0; i < NIdV.
Len(); i++) {
902 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
907 for (
int i = 0; i < NIdV.
Len(); i++) { NIdMap.
AddKey(NIdV[i]); }
908 if (isDir || onlySources) {
909 for (
int i = 0; i < NIdV.
Len(); i++) {
910 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
913 for (
int i = 0; i < NIdV.
Len(); i++) {
914 const TIntPr& Deg = NIdDegH.GetDat(NIdV[i]);
919 for (
int i = 0; i < NIdV.
Len(); i++) {
921 const TNodeI NI = GetNI(NId);
924 for (
int e = 0; e < NI.
GetOutDeg(); e++) {
927 *NIdVPt = Dst; NIdVPt++; deg++; }
929 EAssert(deg == NIdDegH.GetDat(NId).Val2);
930 if (isDir && ! onlySources) {
935 for (
int e = 0; e < NI.
GetInDeg(); e++) {
938 *NIdVPt = Dst; NIdVPt++; deg++; }
940 EAssert(deg == NIdDegH.GetDat(NId).Val1);
943 printf(
" %u edges [%s]\n",
uint(Edges), ExeTm.GetStr());
946 template <
class TNodeData,
bool IsDir>
948 NodeH.Gen(
TMath::Mx(
int(1.1*Nodes), 100));
953 template <
class TNodeData,
bool IsDir>
957 printf(
"Is ok network:\n Check Vec...");
958 for (
uint id = 1;
id < Pool.GetVecs();
id++) {
960 if (! ValV.
Empty()) {
961 EAssert((0<=ValV[0] && ValV[0]<MxNId) || ValV[0]==DelNId);
963 for (
int i = 1; i < ValV.
Len(); i++) {
966 EAssertR((ValV[i-1]<ValV[i]) || (ValV[i-1]==ValV[i] && ValV[i]==DelNId),
967 TStr::Fmt(
"NId1: %d NId2:%d", ValV[i-1](),ValV[i]()));
968 EAssertR((0<=ValV[i] && ValV[i]<MxNId) || ValV[i]==DelNId,
969 TStr::Fmt(
"NId1: %dm MxNId: %d", ValV[i](), MxNId));
970 if (! OnlySources()) {
971 EAssertR(IsNode(ValV[i]) || ValV[i]==DelNId,
972 TStr::Fmt(
"NId1: %dm MxNId: %d", ValV[i](), MxNId)); }
975 printf(
" %s\n", Except->GetStr().CStr());
976 printf(
" vec id: %d, lenght: %d\n",
id, ValV.
Len());
977 for (
int i = 1; i < ValV.
Len(); i++) {
978 printf(
" %d", ValV[i]());
979 if (!((ValV[i-1]<ValV[i]) || (ValV[i-1]==ValV[i] && ValV[i]==DelNId))) { printf(
"*"); }
985 if (
id % 10000 == 0) {
986 printf(
"\r %dk / %dk [%s]",
id/1000, Pool.GetVecs()/1000, ExeTm.
GetStr()); }
988 printf(
"[%s]\n Check Edges...\n", ExeTm.
GetStr());
991 if (! OnlySources()) {
993 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++, Cnt++) {
995 for (
int e = 0; e < NI.GetInDeg(); e++) {
996 if (NI.GetInNId(e) == DelNId) {
continue; }
997 if (! IsEdge(NI.GetInNId(e), NI.GetId())) {
998 printf(
"\nno edge: %d --> %d", NI.GetInNId(e), NI.GetId());
999 printf(
"NId: %d deg %d,%d\n", NI.GetId(), NI.GetOutDeg(), NI.GetInDeg());
1000 for (
int i = 0; i < NI.GetInDeg(); i++) { printf(
" %d", NI.GetInNId(i)); }
1001 TNodeI NI2 = GetNI(NI.GetInNId(e));
1011 for (
int e = 0; e < NI.GetOutDeg(); e++) {
1012 if (NI.GetOutNId(e) == DelNId) {
continue; }
1013 const int InVId = GetNode(NI.GetOutNId(e)).InVId;
1014 int* DstInV = (
int *)Pool.GetValVPt(InVId);
1015 if (BinSearch(DstInV, DstInV+Pool.GetVLen(InVId), NI.GetId()) == NULL) {
1016 printf(
"no edge: %d --> %d", NI.GetId(), NI.GetInNId(e));
1017 printf(
"NId: %d deg %d,%d\n", NI.GetId(), NI.GetOutDeg(), NI.GetInDeg());
1018 for (
int i = 0; i < NI.GetOutDeg(); i++) { printf(
" %d", NI.GetOutNId(i)); }
1019 TNodeI NI2 = GetNI(NI.GetOutNId(e));
1021 for (
int j = 0; j < NI2.
GetInDeg(); j++) { printf(
" %d", NI2.
GetInNId(j)); }
1022 printf(
"\n"); ErrCnt++;
1027 if (ErrCnt > 5) {
FailR(
"error count too large!"); }
1028 if (Cnt % 100000 == 0) {
1036 template <
class TNodeData,
bool IsDir>
1038 if (! Desc.
Empty()) { printf(
"\n%s (%d, %u):\n", Desc.
CStr(), GetNodes(), GetEdges()); }
1039 else { printf(
"\nNodes: %d, Edges: %u\n", GetNodes(), GetEdges()); }
1040 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
1041 printf(
"%d]\n IN %d]", NI.GetId(), NI.GetInDeg());
1042 for (
int i=0; i<NI.GetInDeg(); i++) { printf(
" %d", NI.GetInNId(i)); }
1044 printf(
"\n OUT %d]", NI.GetOutDeg());
1045 for (
int i=0; i<NI.GetOutDeg(); i++) { printf(
" %d", NI.GetOutNId(i)); }
1054 template <
class TNodeData,
bool IsDir>
1056 const bool IsDirected = IsDir;
1058 FOut.
Save(GetNodes());
1059 FOut.
Save(GetEdges());
1060 FOut.
Save(IsDirected);
1061 for (
TNodeI NI = BegNI(); NI < EndNI(); NI++) {
1062 FOut.
Save(NI.GetId());
1063 NI.GetDat().Save(FOut);
1064 FOut.
Save(NI.GetOutDeg());
1065 for (
int i = 0; i < NI.GetOutDeg(); i++) {
1066 FOut.
Save(NI.GetOutNId(i)); }
1068 FOut.
Save(NI.GetInDeg());
1069 for (
int i = 0; i < NI.GetInDeg(); i++) {
1070 FOut.
Save(NI.GetInNId(i)); }
1076 template <
class TNodeData,
bool IsDir>
1081 printf(
"skipping Pool...\n");
1082 TBool FastCopy(SIn);
1083 uint64 _GrowBy, _MxVals, _Vals;
1087 for (
TSize ValN = 0; ValN < _Vals; ValN++) { SIn.
Load(Tmp); }
1088 TInt MxVals(SIn), Vals(SIn);
1090 for (
int ValN = 0; ValN < Vals; ValN++) { SIn.
Load(Offset); }
1091 printf(
"loading Hode hash table...\n");
1096 template <
class TNodeData,
bool IsDir>
1100 {
TInt MxNId(FIn); MxNId.
Save(FOut);
1104 if (! SaveSparseHash) {
1107 NewH.
AddDat(I->Key, I->Dat);
1111 FailR(
"Not implemented");
void Defrag(const bool &OnlyNodeLinks=false)
const TNode & GetNode(const int &NId) const
TPt< TBigNet< TNodeData, IsDir > > PBigNet
TPair< TInt, TInt > TIntPr
Tests (at compile time) if the graph is a network with data on nodes.
void GetInNIdV(int NId, TIntV &OutNIdV) const
int AddUndirNode(int NId, const int &Deg)
const TNodeData & GetSrcNDat() const
#define IAssertR(Cond, Reason)
TNodeDat & GetNDat(const int &NId)
static TStr GetMegaStr(const int &Val)
bool IsIsoNode(const int &NId) const
TNode(const int &InVecId, const int &OutVecId)
static const T & Mx(const T &LVal, const T &RVal)
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
TNodeI & operator=(const TNodeI &NI)
void Save(TSOut &SOut) const
void ReserveNIdInDeg(const int &NId, const int &InDeg)
Reserves memory for node ID NId having InDeg in-edges.
void SaveForDisk(const TStr &OutFNm) const
static PBigNet New(const int &Nodes, const TSize &Edges, const bool &Sources=false)
int GetKeyId(const TKey &Key) const
static void LoadNodeDatH(const TStr &InFNm, TNodeH &NodeH)
Tests (at compile time) if the graph is directed.
void SetOutNIdV(int NId, const TIntV &OutNIdV)
TSizeTy Len() const
Returns the number of elements in the vector.
void Gen(const int &ExpectVals)
int AddEdge(const int &SrcNId, const int &DstNId)
void Clr(const bool &DoDel=true)
TNode & GetNode(const int &NId)
void Save(TSOut &SOut) const
void Save(TSOut &SOut) const
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
bool IsOutNId(const int &NId) const
void Reserve(const int &Nodes, const TSize &Edges)
const TDat & GetDat(const TKey &Key) const
bool operator==(const TEdgeI &EdgeI) const
bool IsUnused() const
Tests whether the node is deleted then it is unused (and its InVId==OutVId==-1)
void Save(TSOut &SOut) const
TSize GetVals() const
Returns the total number of values stored in the vector pool.
TInt InVId
Id of the vector storing nodes that point to the current node.
int GetMxNId() const
Returns an id that is larger than any node id in the network.
TEdgeI(const TEdgeI &EdgeI)
bool Empty() const
Tests whether the vector is empty.
TStr GetFlagStr(const TGraphFlag &GraphFlag)
Returns a string representation of a flag.
TBigNet(const int &Nodes, const TSize &Edges, const bool &Sources=false)
void Save(TSOut &SOut) const
void Dump(const TStr &Desc=TStr()) const
TNodeI GetNI(const int &NId) const
bool IsNode(const int &NId, TNode &Node) const
static PBigNet Load(TSIn &SIn)
void DelKey(const TKey &Key)
int GetInNId(const int &NodeN) const
bool operator==(const TNodeI &NI) const
int GetOutNbrId(const int &NodeN) const
TNodeI(const THashIter &NodeHIter, TVPool *PoolPt)
TVal * GetValVPt(const int &VId) const
Returns pointer to the first element of the vector with id VId.
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
bool IsInNId(const int &NId) const
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
int * GetOutNIdVPt(const int &NId) const
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
TNodeDat Dat
Data associated with the node.
unsigned long long uint64
TNode(const int &InVecId, const int &OutVecId, const TNodeDat &NodeDat)
THash< TInt, TNode > TNodeH
int GetVLen(const int &VId) const
Returns the number of elements in the vector with id VId.
TNodeI GetRndNI(TRnd &Rnd=TInt::Rnd) const
void Clr(bool DoDel=true)
Clears the contents of the pool.
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
bool IsKeyGetDat(const TKey &Key, TDat &Dat) const
bool operator<(const TNodeI &NI) const
int AddKey(const TKey &Key)
bool In(const int &BitN) const
TNodeI & operator++(int)
Increment iterator.
void Incl(const int &BitN)
TBigNet & operator=(const TBigNet &Net)
const TNodeDat & GetNDat(const int &NId) const
bool IsNbrNId(const int &NId) const
TEdgeI & operator=(const TEdgeI &EdgeI)
THashKeyDatI< TInt, TNode > TIter
void Save(const bool &Bool)
TNodeI(const TNodeI &NodeI)
const TNodeData & operator()() const
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
static const int * BinSearch2(const int *Beg, const int *End, const int &Val)
void SetInNIdV(int NId, const TIntV &InNIdV)
void GetNIdV(TIntV &NIdV) const
PNGraph GetNGraph(const bool &RenumberNodes=false) const
void GetKeyV(TVec< TKey > &KeyV) const
PBigNet GetSubGraph(const TIntV &NIdV, const bool &RenumberNodes=false) const
int AddNode(int NId, const int &InDeg, const int &OutDeg)
TDat & AddDat(const TKey &Key)
void InvertFromSources(uint ExpectNodes=0)
static void SaveToDisk(const TStr &InFNm, const TStr &OutFNm, const bool &SaveSparseHash)
int * GetInNIdVPt(const int &NId) const
sentinel, last value for iteration
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
static TStr Fmt(const char *FmtStr,...)
bool IsSorted(const bool &Asc=true) const
Checks whether the vector is sorted in ascending (if Asc=true) or descending (if Asc=false) order...
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types.
static const int * BinSearch(const int *Beg, const int *End, const int &Val)
int GetOutNId(const int &NodeN) const
bool IsNode(const int &NId) const
virtual void Save(TSOut &SOut) const
TEdgeI GetEI(const int &EId) const
bool operator<(const TEdgeI &EdgeI) const
#define EAssertR(Cond, MsgStr)
static void AddSorted(int *Beg, int *End, const int &Val)
void Rewire(TRnd &Rnd=TInt::Rnd)
nodes only store out-edges (but not in-edges). See TBigNet
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a graph of Nodes nodes and Edges edges.
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
TEdgeI(const TNodeI &NodeI, const TNodeI &EndNodeI, const int &EdgeN=0)
int GetRndKeyId(TRnd &Rnd) const
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
int GetRndNId(TRnd &Rnd=TInt::Rnd) const
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &Dir=true) const
PNGraph GetSubNGraph(const TIntV &NIdV) const
static TVec< TVal, TSizeTy > GetV(const TVal &Val1)
Returns a vector on element Val1.
const char * GetStr() const
void GetOutNIdV(int NId, TIntV &OutNIdV) const
bool IsKey(const TKey &Key) const
bool IsVId(const int &VId) const
Tests whether vector of id VId is in the pool.
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
TDat & AddDat(const TKey &Key)
const TKey & GetKey(const int &KeyId) const
Node Network (directed graph, TNGraph with data on nodes only).
TBigNet< TNodeData, IsDir > TNet
const TNodeData & GetDat() const
TInt OutVId
Id of the vector storing nodes that the current node points to.
void ReserveNIdOutDeg(const int &NId, const int &OutDeg)
Reserves memory for node ID NId having OutDeg out-edges.
bool HasFlag(const TGraphFlag &Flag) const
TIter GetI(const TKey &Key) const