6 template <
class PGraph> PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId=0,
const int& DstColId=1);
8 template <
class PGraph> PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId,
const int& DstColId,
const char& Separator);
10 template <
class PGraph> PGraph
LoadEdgeListStr(
const TStr& InFNm,
const int& SrcColId=0,
const int& DstColId=1);
36 template <
class PGraph>
void SavePajek(
const PGraph& Graph,
const TStr& OutFNm);
38 template <
class PGraph>
void SavePajek(
const PGraph& Graph,
const TStr& OutFNm,
const TIntStrH& NIdColorH);
54 template<
class PGraph>
void SaveGViz(
const PGraph& Graph,
const TStr& OutFNm,
const TStr& Desc,
const TIntStrH& NIdLabelH);
67 template <
class PGraph>
70 PGraph Graph = PGraph::TObj::New();
73 if (! Ss.
GetInt(SrcColId, SrcNId) || ! Ss.
GetInt(DstColId, DstNId)) {
continue; }
74 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
75 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
76 Graph->AddEdge(SrcNId, DstNId);
87 template <
class PGraph>
88 PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId,
const int& DstColId,
const char& Separator) {
90 PGraph Graph = PGraph::TObj::New();
93 if (! Ss.
GetInt(SrcColId, SrcNId) || ! Ss.
GetInt(DstColId, DstNId)) {
continue; }
94 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
95 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
96 Graph->AddEdge(SrcNId, DstNId);
108 template <
class PGraph>
111 PGraph Graph = PGraph::TObj::New();
114 const int SrcNId = StrToNIdH.
AddKey(Ss[SrcColId]);
115 const int DstNId = StrToNIdH.
AddKey(Ss[DstColId]);
116 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
117 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
118 Graph->AddEdge(SrcNId, DstNId);
131 template <
class PGraph>
134 PGraph Graph = PGraph::TObj::New();
136 const int SrcNId = StrToNIdH.
AddKey(Ss[SrcColId]);
137 const int DstNId = StrToNIdH.
AddKey(Ss[DstColId]);
138 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
139 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
140 Graph->AddEdge(SrcNId, DstNId);
151 template <
class PGraph>
154 PGraph Graph = PGraph::TObj::New();
156 if (! Ss.
IsInt(0)) {
continue; }
157 const int SrcNId = Ss.
GetInt(0);
158 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
159 for (
int dst = 1; dst < Ss.
Len(); dst++) {
160 const int DstNId = Ss.
GetInt(dst);
161 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
162 Graph->AddEdge(SrcNId, DstNId);
175 template <
class PGraph>
178 PGraph Graph = PGraph::TObj::New();
180 const int SrcNId = StrToNIdH.
AddDatId(Ss[0]);
181 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
182 for (
int dst = 1; dst < Ss.
Len(); dst++) {
183 const int DstNId = StrToNIdH.
AddDatId(Ss[dst]);
184 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
185 Graph->AddEdge(SrcNId, DstNId);
192 template <
class PGraph>
194 PGraph Graph = PGraph::TObj::New();
196 while ((Ss.
Len()==0 || strstr(Ss[0],
"*vertices") == NULL) && ! Ss.
Eof()) {
199 bool EdgeList =
true;
200 EAssert(strstr(Ss[0],
"*vertices") != NULL);
203 if (Ss.
Len()>0 && Ss[0][0] ==
'%') {
continue; }
204 if (strstr(Ss[0],
"*arcslist")!=NULL || strstr(Ss[0],
"*edgeslist")!=NULL) { EdgeList=
false;
break; }
205 if (strstr(Ss[0],
"*arcs")!=NULL || strstr(Ss[0],
"*edges")!=NULL) {
break; }
206 Graph->AddNode(Ss.
GetInt(0));
210 if (Ss.
Len()>0 && Ss[0][0] ==
'%') {
continue; }
211 if (Ss.
Len()>0 && Ss[0][0] ==
'*') {
break; }
218 const int SrcNId = Ss.
GetInt(0);
219 for (
int i = 1; i < Ss.
Len(); i++) {
220 Graph->AddEdge(SrcNId, Ss.
GetInt(i)); }
226 template <
class PGraph>
228 FILE *F = fopen(OutFNm.
CStr(),
"wt");
230 else { fprintf(F,
"# Undirected graph (each unordered pair of nodes is saved once): %s\n", OutFNm.
CStr()); }
231 if (! Desc.
Empty()) { fprintf(F,
"# %s\n", Desc.
CStr()); }
232 fprintf(F,
"# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
234 else { fprintf(F,
"# NodeId\tNodeId\n"); }
235 for (
typename PGraph::TObj::TEdgeI ei = Graph->BegEI(); ei < Graph->EndEI(); ei++) {
236 fprintf(F,
"%d\t%d\n", ei.GetSrcNId(), ei.GetDstNId());
241 template <
class PGraph>
243 TIntH NIdToIdH(Graph->GetNodes(),
true);
244 FILE *F = fopen(OutFNm.
CStr(),
"wt");
245 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
247 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
248 fprintf(F,
"%d \"%d\" ic Red fos 10\n", i+1, NI.GetId());
249 NIdToIdH.
AddDat(NI.GetId(), i+1);
252 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
254 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
256 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
257 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
258 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
259 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
266 template <
class PGraph>
268 TIntH NIdToIdH(Graph->GetNodes(),
true);
269 FILE *F = fopen(OutFNm.
CStr(),
"wt");
270 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
272 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
273 fprintf(F,
"%d \"%d\" ic %s fos 10\n", i+1, NI.GetId(),
274 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
275 NIdToIdH.
AddDat(NI.GetId(), i+1);
278 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
280 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
282 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
283 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
284 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
285 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
293 template <
class PGraph>
295 TIntH NIdToIdH(Graph->GetNodes(),
true);
296 FILE *F = fopen(OutFNm.
CStr(),
"wt");
297 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
299 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
300 fprintf(F,
"%d \"%s\" ic %s fos 10\n", i+1,
301 NIdLabelH.
IsKey(NI.GetId()) ? NIdLabelH.
GetDat(NI.GetId()).CStr() :
TStr::Fmt(
"%d", NI.GetId()).CStr(),
302 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
303 NIdToIdH.
AddDat(NI.GetId(), i+1);
306 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
308 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
310 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
311 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
312 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
313 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
322 template <
class PGraph>
325 TIntH NIdToIdH(Graph->GetNodes(),
true);
326 FILE *F = fopen(OutFNm.
CStr(),
"wt");
327 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
329 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
330 fprintf(F,
"%d \"%s\" ic %s fos 10\n", i+1,
331 NIdLabelH.
IsKey(NI.GetId()) ? NIdLabelH.
GetDat(NI.GetId()).CStr() :
TStr::Fmt(
"%d", NI.GetId()).CStr(),
332 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
333 NIdToIdH.
AddDat(NI.GetId(), i+1);
336 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
338 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
340 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
341 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
342 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
343 fprintf(F,
"%d %d 1 c %s\n", SrcNId, DstNId,
344 EIdColorH.
IsKey(EI.GetId()) ? EIdColorH.
GetDat(EI.GetId()).CStr() :
"Black");
350 template <
class PGraph>
352 FILE *F = fopen(OutFNm.
CStr(),
"wt");
353 TIntSet NIdSet(Graph->GetNodes());
354 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
355 NIdSet.
AddKey(NI.GetId());
357 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
358 const int Src = NIdSet.GetKeyId(EI.GetSrcNId())+1;
359 const int Dst = NIdSet.GetKeyId(EI.GetDstNId())+1;
360 fprintf(F,
"%d\t%d\t1\n", Src, Dst);
362 fprintf(F,
"%d\t%d\t1\n", Dst, Src);
368 template<
class PGraph>
371 FILE *F = fopen(OutFNm.
CStr(),
"wt");
372 if (! Desc.
Empty()) fprintf(F,
"/*****\n%s\n*****/\n\n", Desc.
CStr());
373 if (IsDir) { fprintf(F,
"digraph G {\n"); }
else { fprintf(F,
"graph G {\n"); }
374 fprintf(F,
" graph [splines=false overlap=false]\n");
377 fprintf(F,
" node [shape=ellipse, width=0.3, height=0.3%s]\n", NodeLabels?
"":
", label=\"\"");
380 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
381 if (NIdColorH.
IsKey(NI.GetId())) {
382 fprintf(F,
" %d [style=filled, fillcolor=\"%s\"];\n", NI.GetId(), NIdColorH.
GetDat(NI.GetId()).CStr()); }
384 fprintf(F,
" %d ;\n", NI.GetId());
388 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
389 if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && !NIdColorH.
IsKey(NI.GetId())) {
390 fprintf(F,
"%d;\n", NI.GetId()); }
392 for (
int e = 0; e < NI.GetOutDeg(); e++) {
393 if (! IsDir && NI.GetId() > NI.GetOutNId(e)) {
continue; }
394 fprintf(F,
" %d %s %d;\n", NI.GetId(), IsDir?
"->":
"--", NI.GetOutNId(e));
398 if (! Desc.
Empty()) {
399 fprintf(F,
" label = \"\\n%s\\n\";", Desc.
CStr());
400 fprintf(F,
" fontsize=24;\n");
406 template<
class PGraph>
408 const bool IsDir = Graph->HasFlag(
gfDirected);
409 FILE *F = fopen(OutFNm.
CStr(),
"wt");
410 if (! Desc.
Empty()) fprintf(F,
"/*****\n%s\n*****/\n\n", Desc.
CStr());
411 if (IsDir) { fprintf(F,
"digraph G {\n"); }
else { fprintf(F,
"graph G {\n"); }
412 fprintf(F,
" graph [splines=true overlap=false]\n");
413 fprintf(F,
" node [shape=ellipse, width=0.3, height=0.3]\n");
416 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
417 fprintf(F,
" %d [label=\"%s\"];\n", NI.GetId(), NIdLabelH.
GetDat(NI.GetId()).CStr());
420 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
421 if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && ! NIdLabelH.
IsKey(NI.GetId())) {
422 fprintf(F,
"%d;\n", NI.GetId()); }
424 for (
int e = 0; e < NI.GetOutDeg(); e++) {
425 if (! IsDir && NI.GetId() > NI.GetOutNId(e)) {
continue; }
426 fprintf(F,
" %d %s %d;\n", NI.GetId(), IsDir?
"->":
"--", NI.GetOutNId(e));
430 if (! Desc.
Empty()) {
431 fprintf(F,
" label = \"\\n%s\\n\";", Desc.
CStr());
432 fprintf(F,
" fontsize=24;\n");
void SavePajek(const PGraph &Graph, const TStr &OutFNm)
Saves a graph in a Pajek .NET format.
void SaveGViz(const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Save a graph in GraphVizp .DOT format.
PGraph LoadConnListStr(const TStr &InFNm, TStrHash< TInt > &StrToNIdH)
Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in...
void ToLc()
Transforms the current line to lower case.
PGraph LoadEdgeList(const TStr &InFNm, const int &SrcColId=0, const int &DstColId=1)
Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace...
TDat & AddDatId(const char *Key)
bool GetInt(const int &FldN, int &Val) const
If the field FldN is an integer its value is returned in Val and the function returns true...
THash< TInt, TStr > TIntStrH
const TDat & GetDat(const TKey &Key) const
PGraph LoadPajek(const TStr &InFNm)
Loads a (directed, undirected or multi) graph from Pajek .PAJ format file.
PGraph LoadConnList(const TStr &InFNm)
Loads a (directed, undirected or multi) graph from a text file InFNm with 1 node and all its edges in...
TVec< PNGraph > LoadDyNetGraphV(const TStr &FNm)
For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php)
have explicit edges (multigraph): TNEGraph, TNodeEdgeNet
bool Eof() const
Checks for end of file.
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Whitespace (space or tab) separated.
PGraph LoadEdgeListStr(const TStr &InFNm, const int &SrcColId=0, const int &DstColId=1)
Loads a (directed, undirected or multi) graph from a text file InFNm with 1 edge per line (whitespace...
int AddKey(const TKey &Key)
int Len() const
Returns the number of fields in the current line.
void SaveEdgeList(const PGraph &Graph, const TStr &OutFNm, const TStr &Desc=TStr())
Saves a graph into a text file. Each line contains two columns and encodes a single edge:
int AddKey(const char *Key)
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
static TStr Fmt(const char *FmtStr,...)
bool Next()
Loads next line from the input file.
PNGraph LoadDyNet(const TStr &FNm)
For more info see ORA Network Analysis Data (http://www.casos.cs.cmu.edu/computational_tools/data2.php)
bool IsKey(const TKey &Key) const
void SaveMatlabSparseMtx(const PGraph &Graph, const TStr &OutFNm)
Saves a graph in a MATLAB sparse matrix format.
bool IsInt(const int &FldN) const
Checks whether fields FldN is an integer.
TDat & AddDat(const TKey &Key)
Vector is a sequence TVal objects representing an array that can change in size.