wolfram mathematica - Re-layout graph after small modification while preserving features of original layout -
is there simple way following in mathematica 8?
- construct graph, , display using graph layout.
- modify graph (e.g. add or remove edge or vertex).
- re-compute layout starting original layout, in such way the "shape" of object more or less preserved. e.g. re-run spring-electric layout algorithm starting coordinates of previous layout.
if graph hasn't changed between 2 displays, layout shouldn't change either (or minimally). using display of new graph
or graphplot
both acceptable.
edit: in essence need similar layouts similar graphs. obtain similar graphs modifying existing one, may have been laid out, generic solution acceptable.
edit 2: here's example of kind of thing useful. go http://ccl.northwestern.edu/netlogo/models/giantcomponent , click "run in browser" (requires java). click setup click go. can see graph evolve. if in mathematica, each of successive graphs different, , difficult see same graph evolving. in several applications it's quite useful able visualize small changes graph such. if many successive changes done, re-computing layout must, fading or highlighting edges not sufficient. again, example: i'm not trying use mathematica animate graph, or visualize emergence of giant component.
here 2 basic approaches altering graphs in mma 8.0. first relies on highlightgraph
, in particular on graphhighlightstyle -> "dehighlighthide"
. second approach uses vertexcoordinates of graph in future variants of graph.
we'll discuss deletion separately addition because involve different methods.
[p.s. : made several edits answer in make clearer.]
first data:
edges={1\[undirectededge]8,1\[undirectededge]11,1\[undirectededge]18,1\[undirectededge]19,1\[undirectededge]21,1\[undirectededge]25,1\[undirectededge]26,1\[undirectededge]34,1\[undirectededge]37,1\[undirectededge]38,4\[undirectededge]11,4\[undirectededge]12,4\[undirectededge]26,4\[undirectededge]27,4\[undirectededge]47,4\[undirectededge]56,4\[undirectededge]57,4\[undirectededge]96,4\[undirectededge]117,5\[undirectededge]11,5\[undirectededge]18,7\[undirectededge]21,7\[undirectededge]25,7\[undirectededge]34,7\[undirectededge]55,7\[undirectededge]76,8\[undirectededge]11,26\[undirectededge]29,26\[undirectededge]49,26\[undirectededge]52,26\[undirectededge]111,27\[undirectededge]28,27\[undirectededge]51,42\[undirectededge]47,49\[undirectededge]97,51\[undirectededge]96}
here initial graph:
g = graph[edges, vertexlabels -> "name", imagepadding -> 10, imagesize -> 500]
"deleting" graph edge without changing overall appearance of graph.
let's begin remove edge (4,11) located @ center of graph. remainingedgesandvertices
contains vertices , initial edges exception of edge (4,11).
remainingedgesandvertices = join[vertexlist[g], complement[edgelist[g], {4 \[undirectededge] 11}]]
let's "delete" (i.e. hide) edge (4,11):
highlightgraph[g, remainingedgesandvertices, vertexlabels -> "name", imagepadding -> 10, graphhighlightstyle -> "dehighlighthide", imagesize -> 500]
if had removed edge (4, 11) graph have radically changed appearance.
graph[complement[edges, {4 \[undirectededge] 11}], vertexlabels -> "name", imagepadding -> 10, imagesize -> 500]
"adding" graph edge without changing overall appearance of graph.
adding graph edge more challenging. there 2 ways come mind. method used here works backwards. include new edge first in hidden form , uncover later. initial graph hidden, "to-be-added" edge in layout similar of graph "new" edge. reason this: in fact same graph: show different numbers of edges.
g2 = graph[append[edges, 42 \[undirectededge] 37], vertexlabels -> "name", imagepadding -> 10, imagesize -> 500] highlightgraph[g2, join[complement[edgelist[g2], {42 \[undirectededge] 37}], vertexlist[g2]], vertexlabels -> "name", imagepadding -> 10, graphhighlightstyle -> "dehighlighthide"]
now show graph "new edge" added.
this looks different figure 1. seems natural extension of fig. 4.
adding new vertices , edges on-the-fly
there way add edges (and vertices) while maintaining overall appearance. inspired sjoerd wrote in response.
let's reserve point {0,0} future vertex 99. add point vertexcoordinates
g2:
vc = vertexcoordinates -> append[absoluteoptions[g2, vertexcoordinates][[2]], {0, 0}]
now let's see looks like. g3 g2 additional vertex (999) , edge (4,99).
g3 = graph[append[edgelist [g2], 4 \[undirectededge] 999], vc, vertexlabels -> "name", imagepadding -> 10, graphhighlightstyle -> "dehighlighthide", imagesize -> 500]
this procedure allows add new edges , vertices move forward. trial , error needed ensure new vertices located in suitable position.
adding edge (without new vertex) easier: add new edge , use vertexcoordinates
prior graph.
you should able delete edges graph using same approach (using same vertexcoordinates
).
Comments
Post a Comment