From: Emden Gansner Date: Fri, 2 Oct 2015 20:46:22 +0000 (-0400) Subject: Add gvpr scripts for checking if a graph is bipartite, and converting a bipartite... X-Git-Tag: TRAVIS_CI_BUILD_EXPERIMENTAL~107 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=779477cffd7d45168c63522e372e6763be9a7c2b;p=graphviz Add gvpr scripts for checking if a graph is bipartite, and converting a bipartite graph into an ordinary graph by using nodes on one side as edges. --- diff --git a/cmd/gvpr/lib/binduce b/cmd/gvpr/lib/binduce new file mode 100644 index 000000000..9e987c33e --- /dev/null +++ b/cmd/gvpr/lib/binduce @@ -0,0 +1,46 @@ +/* Given a bipartite graph, induce a non-bipartite graph. + * argv[0]="name=value" This is used to identify the nodes used + * to induce edges. If aget(n,name) == value, + * if deg(n) == 1, delete + * if deg(n) == 2, delete and connect to neighbor with edge + * if deg(n) > 2, delete and add edge between all pairs of neighbors + * Add weights to edge. + */ +BEGIN{ + int i, cnt; + int wt[edge_t]; + string values[int]; + node_t nbrs[int]; + edge_t e; + tokens(ARGV[0],values,"="); + string aname = values[0]; + string value = values[1]; + printf(2, "%s=%s\n", aname, value); +} +N[aget($,aname)==value] { + if ($.degree > 1) { + cnt = 0; + for (e = fstedge($); e; e = nxtedge(e, $)) + nbrs[cnt++] = opp(e,$); + for (i = 0; i < cnt-1; i++) { + if (e = isEdge(nbrs[i],nbrs[i+1],"")) { + wt[e] += 1; + } + else if ($G.directed && (e = isEdge(nbrs[i+1],nbrs[i],""))) { + wt[e] += 1; + } + else { + e = edge(nbrs[i],nbrs[i+1],""); + wt[e] = 1; + } + } + unset(nbrs); + } + delete($G,$); +} +END_G{ + printf(2, "#nodes = %d\n", $.n_nodes); + for (wt[e]) { + e.weight = sprintf ("%d", wt[e]); + } +} diff --git a/cmd/gvpr/lib/bipart b/cmd/gvpr/lib/bipart new file mode 100644 index 000000000..959f80478 --- /dev/null +++ b/cmd/gvpr/lib/bipart @@ -0,0 +1,27 @@ +/* Determine if a graph is bipartite or not. + */ +BEG_G{ + int vc, c, color[node_t]; + node_t v; + edge_t e; + $tvtype = TV_dfs; + $tvroot = fstnode($); +} +N{ + if ($tvedge == NULL) + color[$] = 1; + if (color[$] == 1) + c = 2; + else + c = 1; + for (e = fstedge($); e; e = nxtedge(e,$)) { + v = opp(e,$); + vc = color[v]; + if (vc == 0) + color[v] = c; + else if (vc != c) { + printf(2, "Not bipartite\n"); + exit(1); + } + } +}