Tutorial¶
Preliminaries¶
In order to use the graph_snapshot package, a basic knowledge of the networkx Graph concept ist required. So make sure to be able to work with graphs.
Workflow¶
First, import networkx and graph_snapshot and create a Graph:
>>> import networkx as nx
>>> import graph_snapshot as gs
>>> G = nx.Graph()
Now, do something with the graph, e.g. add edges or perform an algorithm:
>>> edges = [("a","b", {"len": 5}), ("c","b", {"len": 7}), ("a","d", {"len": 3}), ("d","b", {"len": 4}), ("a","c", {"len": 4}), ("a", "f", {"len": 2}), ("b", "f", {"len": 2})]
>>> G.add_edges_from(edges, color = 'black')
Everytime you want the current state of the algorithm to be visualized, you can ‘snapshot’ the graph:
>>> gs.snapshot(G)
>>> G.add_edge("c", "f", len = 2, color = 'red')
>>> gs.snapshot(G)
Once you are done with your algorithm, it is time to create the tikz code from your snapshots:
>>> gs.compile("directory_where_you_want_the_tikz_files", lenAsLabel = True)
The directory should look as follows:
$ ls directory_where_you_want_the_tikz_files
graph0.dot graph0.tex graph1.dot graph1.tex
You can then manually include the tex files into your document. The following two images are created by calling:
>>> gs.standalone("directory_where_you_want_the_tikz_files")
If you want to include the tex files into your beamer class documentation you can instead use following to create a beamer slide containing all images:
>>> gs.beamer_slide("directory_where_you_want_the_tikz_files")
To create a single latex document containg all images call:
>>> gs.latex_document("directory_where_you_want_the_tikz_files")
The last two mentioned functions also provide several options, including: setting a title for the created beamer slide or latex document, define a path where the generated latex files will be placed or caption your images by providing a caption list as following:
>>> gs.beamer_slide("directory_where_you_want_the_tikz_files", "title", "directory_where_returndes_tex_file_is_placed", ["caption1","caption2"])
>>> gs.latex_document("directory_where_you_want_the_tikz_files", "title", "directory_where_returndes_tex_file_is_placed", ["caption1","caption2"])
Configuration¶
Using multiple “Screenshot series”: graph_list¶
Sometimes you might want to visualize two algorithms in the same file. Then you should make use of the graph_list concept. First, specify a list for each algorithm:
>>> algorithm1_list = []
>>> algorithm2_list = []
When calling gs.snapshot, decide for one list, e.g.:
>>> gs.snapshot(G, graph_list = algorithm1_list)
Finally, you can compile each list on it’s own:
>>> gs.compile("algorithm1_dir", graph_list = algorithm1_list)
>>> gs.compile("algorithm2_dir", graph_list = algorithm2_list)
Keywordarguments for Edges/Nodes¶
As you have already seen, you can pass keywordarguments to graph edges. The same concept works for nodes as well. Any keyword that is understandable by tikz will have an effect on the resulting image. If you want to add specific style to your edges, you can also define your own tikz style in the document header and use that style as an edge attribute. There are also some edge keywords that have specific meaning.
- len specifies the length of the edge. The graph layout algorithm will try to fit this lenght as close as possible, but as you can see in the second figure of the last section, if you hurt the triangle inequality, this can’t work.
- weight specifies how much this edge is considered in computing the layout. In general, this option will not be needed.
- label has the same meaning as in tikz, however it can be overwritten by the len argument if you set lenAsLabel = True when calling the compile function.
Keywordarguments for compile¶
We have already mentioned the graph_list and lenAsLabel keywords in the previous two subsections. See the reference for a detailed description of the other arguments.