18 template <
class PGraph> PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId=0,
const int& DstColId=1);
20 template <
class PGraph> PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId,
const int& DstColId,
const char& Separator);
24 template <
class PGraph> PGraph
LoadEdgeListStr(
const TStr& InFNm,
const int& SrcColId=0,
const int& DstColId=1);
52 template <
class PGraph>
void SavePajek(
const PGraph& Graph,
const TStr& OutFNm);
54 template <
class PGraph>
void SavePajek(
const PGraph& Graph,
const TStr& OutFNm,
const TIntStrH& NIdColorH);
70 template<
class PGraph>
void SaveGViz(
const PGraph& Graph,
const TStr& OutFNm,
const TStr& Desc,
const TIntStrH& NIdLabelH);
83 template <
class PGraph>
86 PGraph Graph = PGraph::TObj::New();
90 if (! Ss.
GetInt(SrcColId, SrcNId) || ! Ss.
GetInt(DstColId, DstNId)) {
continue; }
91 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
92 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
93 Graph->AddEdge(SrcNId, DstNId);
104 template <
class PGraph>
105 PGraph
LoadEdgeList(
const TStr& InFNm,
const int& SrcColId,
const int& DstColId,
const char& Separator) {
107 PGraph Graph = PGraph::TObj::New();
110 if (! Ss.
GetInt(SrcColId, SrcNId) || ! Ss.
GetInt(DstColId, DstNId)) {
continue; }
111 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
112 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
113 Graph->AddEdge(SrcNId, DstNId);
125 template <
class PGraph>
128 PGraph Graph = PGraph::TObj::New();
131 const int SrcNId = StrToNIdH.
AddKey(Ss[SrcColId]);
132 const int DstNId = StrToNIdH.
AddKey(Ss[DstColId]);
133 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
134 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
135 Graph->AddEdge(SrcNId, DstNId);
148 template <
class PGraph>
151 PGraph Graph = PGraph::TObj::New();
153 const int SrcNId = StrToNIdH.
AddKey(Ss[SrcColId]);
154 const int DstNId = StrToNIdH.
AddKey(Ss[DstColId]);
155 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
156 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
157 Graph->AddEdge(SrcNId, DstNId);
168 template <
class PGraph>
171 PGraph Graph = PGraph::TObj::New();
173 if (! Ss.
IsInt(0)) {
continue; }
174 const int SrcNId = Ss.
GetInt(0);
175 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
176 for (
int dst = 1; dst < Ss.
Len(); dst++) {
177 const int DstNId = Ss.
GetInt(dst);
178 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
179 Graph->AddEdge(SrcNId, DstNId);
192 template <
class PGraph>
195 PGraph Graph = PGraph::TObj::New();
197 const int SrcNId = StrToNIdH.
AddDatId(Ss[0]);
198 if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); }
199 for (
int dst = 1; dst < Ss.
Len(); dst++) {
200 const int DstNId = StrToNIdH.
AddDatId(Ss[dst]);
201 if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); }
202 Graph->AddEdge(SrcNId, DstNId);
209 template <
class PGraph>
211 PGraph Graph = PGraph::TObj::New();
213 while ((Ss.
Len()==0 || strstr(Ss[0],
"*vertices") == NULL) && ! Ss.
Eof()) {
216 bool EdgeList =
true;
217 EAssert(strstr(Ss[0],
"*vertices") != NULL);
220 if (Ss.
Len()>0 && Ss[0][0] ==
'%') {
continue; }
221 if (strstr(Ss[0],
"*arcslist")!=NULL || strstr(Ss[0],
"*edgeslist")!=NULL) { EdgeList=
false;
break; }
222 if (strstr(Ss[0],
"*arcs")!=NULL || strstr(Ss[0],
"*edges")!=NULL) {
break; }
223 Graph->AddNode(Ss.
GetInt(0));
227 if (Ss.
Len()>0 && Ss[0][0] ==
'%') {
continue; }
228 if (Ss.
Len()>0 && Ss[0][0] ==
'*') {
break; }
235 const int SrcNId = Ss.
GetInt(0);
236 for (
int i = 1; i < Ss.
Len(); i++) {
237 Graph->AddEdge(SrcNId, Ss.
GetInt(i)); }
243 template <
class PGraph>
245 FILE *F = fopen(OutFNm.
CStr(),
"wt");
247 else { fprintf(F,
"# Undirected graph (each unordered pair of nodes is saved once): %s\n", OutFNm.
CStr()); }
248 if (! Desc.
Empty()) { fprintf(F,
"# %s\n", Desc.
CStr()); }
249 fprintf(F,
"# Nodes: %d Edges: %d\n", Graph->GetNodes(), Graph->GetEdges());
251 else { fprintf(F,
"# NodeId\tNodeId\n"); }
252 for (
typename PGraph::TObj::TEdgeI ei = Graph->BegEI(); ei < Graph->EndEI(); ei++) {
253 fprintf(F,
"%d\t%d\n", ei.GetSrcNId(), ei.GetDstNId());
258 template <
class PGraph>
260 TIntH NIdToIdH(Graph->GetNodes(),
true);
261 FILE *F = fopen(OutFNm.
CStr(),
"wt");
262 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
264 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
265 fprintf(F,
"%d \"%d\" ic Red fos 10\n", i+1, NI.GetId());
266 NIdToIdH.
AddDat(NI.GetId(), i+1);
269 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
271 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
273 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
274 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
275 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
276 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
283 template <
class PGraph>
285 TIntH NIdToIdH(Graph->GetNodes(),
true);
286 FILE *F = fopen(OutFNm.
CStr(),
"wt");
287 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
289 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
290 fprintf(F,
"%d \"%d\" ic %s fos 10\n", i+1, NI.GetId(),
291 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
292 NIdToIdH.
AddDat(NI.GetId(), i+1);
295 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
297 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
299 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
300 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
301 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
302 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
310 template <
class PGraph>
312 TIntH NIdToIdH(Graph->GetNodes(),
true);
313 FILE *F = fopen(OutFNm.
CStr(),
"wt");
314 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
316 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
317 fprintf(F,
"%d \"%s\" ic %s fos 10\n", i+1,
318 NIdLabelH.
IsKey(NI.GetId()) ? NIdLabelH.
GetDat(NI.GetId()).CStr() :
TStr::Fmt(
"%d", NI.GetId()).CStr(),
319 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
320 NIdToIdH.
AddDat(NI.GetId(), i+1);
323 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
325 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
327 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
328 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
329 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
330 fprintf(F,
"%d %d %d c Black\n", SrcNId, DstNId, 1);
339 template <
class PGraph>
342 TIntH NIdToIdH(Graph->GetNodes(),
true);
343 FILE *F = fopen(OutFNm.
CStr(),
"wt");
344 fprintf(F,
"*Vertices %d\n", Graph->GetNodes());
346 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, i++) {
347 fprintf(F,
"%d \"%s\" ic %s fos 10\n", i+1,
348 NIdLabelH.
IsKey(NI.GetId()) ? NIdLabelH.
GetDat(NI.GetId()).CStr() :
TStr::Fmt(
"%d", NI.GetId()).CStr(),
349 NIdColorH.
IsKey(NI.GetId()) ? NIdColorH.
GetDat(NI.GetId()).CStr() :
"Red");
350 NIdToIdH.
AddDat(NI.GetId(), i+1);
353 fprintf(F,
"*Arcs %d\n", Graph->GetEdges()); }
355 fprintf(F,
"*Edges %d\n", Graph->GetEdges());
357 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
358 const int SrcNId = NIdToIdH.GetDat(EI.GetSrcNId());
359 const int DstNId = NIdToIdH.GetDat(EI.GetDstNId());
360 fprintf(F,
"%d %d 1 c %s\n", SrcNId, DstNId,
361 EIdColorH.
IsKey(EI.GetId()) ? EIdColorH.
GetDat(EI.GetId()).CStr() :
"Black");
367 template <
class PGraph>
369 FILE *F = fopen(OutFNm.
CStr(),
"wt");
370 TIntSet NIdSet(Graph->GetNodes());
371 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
372 NIdSet.
AddKey(NI.GetId());
374 for (
typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
375 const int Src = NIdSet.GetKeyId(EI.GetSrcNId())+1;
376 const int Dst = NIdSet.GetKeyId(EI.GetDstNId())+1;
377 fprintf(F,
"%d\t%d\t1\n", Src, Dst);
379 fprintf(F,
"%d\t%d\t1\n", Dst, Src);
385 template<
class PGraph>
388 FILE *F = fopen(OutFNm.
CStr(),
"wt");
389 if (! Desc.
Empty()) fprintf(F,
"/*****\n%s\n*****/\n\n", Desc.
CStr());
390 if (IsDir) { fprintf(F,
"digraph G {\n"); }
else { fprintf(F,
"graph G {\n"); }
391 fprintf(F,
" graph [splines=false overlap=false]\n");
394 fprintf(F,
" node [shape=ellipse, width=0.3, height=0.3%s]\n", NodeLabels?
"":
", label=\"\"");
397 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
398 if (NIdColorH.
IsKey(NI.GetId())) {
399 fprintf(F,
" %d [style=filled, fillcolor=\"%s\"];\n", NI.GetId(), NIdColorH.
GetDat(NI.GetId()).CStr()); }
401 fprintf(F,
" %d ;\n", NI.GetId());
405 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
406 if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && !NIdColorH.
IsKey(NI.GetId())) {
407 fprintf(F,
"%d;\n", NI.GetId()); }
409 for (
int e = 0; e < NI.GetOutDeg(); e++) {
410 if (! IsDir && NI.GetId() > NI.GetOutNId(e)) {
continue; }
411 fprintf(F,
" %d %s %d;\n", NI.GetId(), IsDir?
"->":
"--", NI.GetOutNId(e));
415 if (! Desc.
Empty()) {
416 fprintf(F,
" label = \"\\n%s\\n\";", Desc.
CStr());
417 fprintf(F,
" fontsize=24;\n");
423 template<
class PGraph>
425 const bool IsDir = Graph->HasFlag(
gfDirected);
426 FILE *F = fopen(OutFNm.
CStr(),
"wt");
427 if (! Desc.
Empty()) fprintf(F,
"/*****\n%s\n*****/\n\n", Desc.
CStr());
428 if (IsDir) { fprintf(F,
"digraph G {\n"); }
else { fprintf(F,
"graph G {\n"); }
429 fprintf(F,
" graph [splines=true overlap=false]\n");
430 fprintf(F,
" node [shape=ellipse, width=0.3, height=0.3]\n");
433 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
434 fprintf(F,
" %d [label=\"%s\"];\n", NI.GetId(), NIdLabelH.
GetDat(NI.GetId()).CStr());
437 for (
typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
438 if (NI.GetOutDeg()==0 && NI.GetInDeg()==0 && ! NIdLabelH.
IsKey(NI.GetId())) {
439 fprintf(F,
"%d;\n", NI.GetId()); }
441 for (
int e = 0; e < NI.GetOutDeg(); e++) {
442 if (! IsDir && NI.GetId() > NI.GetOutNId(e)) {
continue; }
443 fprintf(F,
" %d %s %d;\n", NI.GetId(), IsDir?
"->":
"--", NI.GetOutNId(e));
447 if (! Desc.
Empty()) {
448 fprintf(F,
" label = \"\\n%s\\n\";", Desc.
CStr());
449 fprintf(F,
" fontsize=24;\n");
void SavePajek(const PGraph &Graph, const TStr &OutFNm)
Saves a graph in a Pajek .NET format.
void SaveEdgeListNet(const PNEANet &Graph, const TStr &OutFNm, const TStr &Desc)
Saves a network into a text file. Each line encodes either an edge or a node, along with its attribut...
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.
const TStr STR_TYPE_PREFIX
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
const TStr FLT_TYPE_PREFIX
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). ...
PNEANet LoadEdgeListNet(const TStr &InFNm, const char &Separator)
Loads a network from the text file InFNm with 1 node/edge per line ('Separator' separated columns...
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)
const TStr INT_TYPE_PREFIX
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.