Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SaveLEDA Functionality #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions snap-core/gio.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <map>

/////////////////////////////////////////////////
// Loading and saving graphs from/to various file formats.
namespace TSnap {
Expand Down Expand Up @@ -347,6 +349,59 @@ void SaveMatlabSparseMtx(const PGraph& Graph, const TStr& OutFNm) {
fclose(F);
}

// Output has three sections: header, nodes section, and edges section
// See http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html
// for details...
template <class PGraph>
void SaveLEDA(const PGraph& Graph, const TStr& OutFNm) {
FILE *F = fopen(OutFNm.CStr(), "wt");
TIntSet NIdSet(Graph->GetNodes());
fprintf(F, "#header section\n");
fprintf(F, "LEDA.GRAPH\n");
fprintf(F, "int\n"); // We assume the nodes are integers (as given by PGraph)
fprintf(F, "int\n"); // We also assume the edges are integers
if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
fprintf(F, "-1\n");
}
else {
fprintf(F, "-2\n");
}
fprintf(F, "#nodes section\n");
fprintf(F, "%d\n", Graph->GetNodes());
for(typename PGraph::TObj::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
fprintf(F, "|{%d}|\n", NI.GetId());
NIdSet.AddKey(NI.GetId());
}
fprintf(F, "\n#edges section\n");
fprintf(F, "%d\n", Graph->GetEdges());

int i;
// For directed graphs, we need keep track of reversal edges
std::map<int, std::map<int, int> > EdgeMap; // EdgeMap[Src][Dst] = Id of Edge from Src to Dst
if (HasGraphFlag(typename PGraph::TObj, gfDirected)) {
// We first map all the edges to discrete indices...
i = 1;
for(typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
const int Src = NIdSet.GetKeyId(EI.GetSrcNId()) + 1;
const int Dst = NIdSet.GetKeyId(EI.GetDstNId()) + 1;
EdgeMap[Src][Dst] = i;
i++;
}
}
i = 1;
for(typename PGraph::TObj::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
const int Src = NIdSet.GetKeyId(EI.GetSrcNId()) + 1;
const int Dst = NIdSet.GetKeyId(EI.GetDstNId()) + 1;
int ReverseEdge = 0;
if (HasGraphFlag(typename PGraph::TObj, gfDirected) && Graph->IsEdge(Dst, Src) && Src!=Dst) {
ReverseEdge = EdgeMap[Dst][Src]; //...then use our map to print the appropriate reversal edge
}
fprintf(F, "%d %d %d |{%d}|\n", Src, Dst, ReverseEdge, i);
i++;
}
fclose(F);
}

template<class PGraph>
void SaveGViz(const PGraph& Graph, const TStr& OutFNm, const TStr& Desc, const bool& NodeLabels, const TIntStrH& NIdColorH) {
const bool IsDir = HasGraphFlag(typename PGraph::TObj, gfDirected);
Expand Down