University of Sydney School of Computer Science COMP5048 Week 3 Tutorial By: Dr. Amyra Meidiana

University of Sydney
School of Computer Science
COMP5048 Week 3 Tutorial
By: Dr. Amyra Meidiana
1. Organic layout with yEd
yEd is a graph visualisation software written in Java. While the source code for yFiles, the library
behind yEd, is proprietary, the yEd GUI can be used free of charge.
1.1. Installation instructions
1. Go to the yEd website at https://www.yworks.com/products/yed/download and
select the appropriate package for your system from the download page.
2. Run the installer or uncompress the package. For compressed packages, yEd commands can
now be executed from the directory where the archive has been uncompressed.
1.2. Basics and Data Loading
1. Run the yEd executable.
2. Choose “Open” and then select les_miserables.gml from additional materials – make
sure “All Files” is selected in “Files of Type” if you cannot see the file. You may see only a
single node in the middle of the screen, as no layout is defined for the graph yet.
1.3. Organic Layout
The Organic layout is a variation of the force-directed layout.
1. After the graph is loaded, click “Layout” → “Organic”. This is a force-directed layout and a
dialog window containing options for the layout will open. For now, simply click “OK” to
produce a layout with default parameters.
2. You may notice excessive node/edge overlaps in the image. Open the Organic layout dialog
window again, tick the box next to “Avoid Node/Edge Overlaps”, and click OK.
3. If you still notice excessive node overlaps, try increasing the preferred edge length and/or
minimal node distance, or select other options. Experiment with different parameters.
Figure 1: Organic layout example
1.4. Customise Visualisation
1. Left click drag to select all nodes. You may need to zoom by pressing ctrl while scrolling
to be able to select all nodes.
2. On the right sidebar, double click the triangle shape.
3. Right click on the circle shape and click “Apply”. The nodes should now be triangular.
4. You can change the sizes and colours from “Properties View” on the right sidebar.
5. Export a PNG of your graph by clicking File → Export…
Figure 2: Triangular nodes
2. Fruchterman-Reingold and FM3 with Tulip
Tulip is a graph visualisation software written in C++.
2.1. Installation
1. Go to the Tulip homepage at http://tulip.labri.fr/TulipDrupal/
2. Click the button to download the latest release for your system. If the default downloaded
version is not the correct version compatible with your system, click on “All files and
versions”, and locate the correct installer for your operating system. (Note: if you are on a
Unix-based machine and are downloading the AppImage, you may need to change the mode
of the file to be executable after downloading.)
2.2. Basics and Data Loading
1. Open the Tulip executable.
2. If prompted, click on the green triangle to open Tulip Perspectives. Otherwise, go to Step 3.
3. Click File → Open and open les_miserables.gml.
4. On the right-hand side, you will see the drawing of the graph in “Navigate in graph” mode.
2.3. Fruchterman Reingold Layout
The Fruchterman Reingold (FR) layout is one of the earliest force-directed algorithms.
1. Under the “Algorithms” sidebar on the left, scroll down to the Layout section. Here, you
have a wide selection of layout algorithms
2. Under “Force Directed”, scroll down to “Fruchterman Reingold”. Click on the green
triangle. If you only see a single dot, you may need to randomise the layout first (find the
“Random” layout and click the corresponding triangle) before running FR again.
3. Save a snapshot by clicking the camera icon below the visualisation. You can also export a
GML file by clicking “Export” on the left and entering a .gml filename.
Figure 3: Example FR layout
2.4. Kamada Kawai Layout
The Kamada Kawai (KK) layout is a stress-based layout algorithm.
1. Scroll down to “Kamada Kawai” and click on the green triangle.
2. Open the can_144.gml file and compare the visualisation computed using FR and KK.
Figure 4: Example KK layout
2.5. FM3 layout
The FM3 layout is a multi-level force directed algorithm, similar to FADE, designed for more
efficient visualisation of large graphs.
1. Open lastfm_sub.gml in Tulip and try visualising it using FR and/or KK. You will likely
notice that the time taken to compute the layout is longer than for the smaller graphs.
2. Scroll up to FM^3 and click on the green triangle. The layout should take much shorter to
compute.
3. Click the gear next to FM^3 – this will give you the full options of the layout algorithm, and
hovering over each option would give a short description of the option. For now, try the
different options for Page Format and click the green triangle again.
4. Try varying the other parameters until you get a layout you prefer.
Figure 5: FM3 layout
2.4. Customise visualisation
1. To go to “Display node or edge properties” mode, click the cursor+question mark button on
the top bar. Click on a node or edge to display its properties in this mode.
2. Expand the graph sub-window to show all of the icons in the bottom toolbar; hover over
each button to check their functions. These can be used to change the appearance of the
graph, e.g. show/hide nodes, edges, and labels, change node/edge colours/shapes, etc.
3. Change the shape of the nodes and hide the labels. You can try to change other properties
too, like the sizes and colours of nodes/edges/labels.
4. Save a screenshot by clicking the camera icon.
Figure 6: Displaying a node property in Tulip
Figure 7: Tulip toolbar. Red: take screenshot. Green: hide/show labels. Blue: change node shape
3. Spring layout with NetworkX
NetworkX is a Python software for analysis of complex networks. Documentation and detailed
information can be read from https://networkx.github.io
3.1. Installation Instructions
1. Install Python if you haven’t yet.
2. Make sure to have setuptools, numpy, and matplotlib installed first using pip (e.g. pip
install setuptools, or pip install -U numpy to update an existing installation)
3. Issue the command: pip install networkx
3.2. Basics and Data Loading
1. Open a Python console.
2. Import the networkx package before using it, then create an empty graph:
import networkx as nx
G=nx.Graph()
3. To add a node to the graph, use the add_node function and pass in the ID of the node:
G.add_node(1)
4. To add an edge, use add_edge and pass in the ID of the endpoint nodes:
G.add_edge(1,2)
5. NetworkX contains a number of functions to load graphs from graph files. For example, use:
nx.read_gml to load can_144.gml: G = nx.read_gml( can_144.gml ) “ ”
6. To get the number of nodes and edges, use G.number_of_nodes() and
G.number_of_edges() respectively.
3.3. Spring Layout
The spring layout in NetworkX is an implementation of the Fruchterman Reingold algorithm.
1. Import Matplotlib’s plot interface: import matplotlib.pyplot as plt
2. Import a sample graph: G=nx.karate_club_graph()
3. Compute a layout using the spring layout. Note that this does not show the drawing yet.
nx.draw_spring(G)
4. Display the layout in a pop-up window: plt.show()
5. Save an image using the save icon in the pop-up window.
Figure 8: NetworkX layout pop-out window
3.4. Customise Visualisation
1. Graph objects in NetworkX stores node and edge properties as dictionaries that are
accessible using the names of the nodes. For example, examine the properties of the node
with ID 1 in the karate club graph, which lists the club this node belongs to: G.nodes[1]
2. There are two clubs the nodes in the karate club graph belongs to, Mr. Hi and Officer. We
can change the appearance of the nodes based on this property. To change the colour of a
node, first create a list that assigns the hex code for red for Mr. Hi and blue for Officer:
col_list = [“#ff0000” if G.nodes[n][ club ]== Mr. Hi else “#0000ff” for n in “ ” ” ”
G.nodes]
3. Pass the colours to the draw_spring funciton using the node_color argument:
nx.draw_spring(G, node_color = col_list)
4. Display the layout using plt.show(). Compared to the result from Exercise 3.3, the nodes
should now have different colours based on the club membership.
5. To change the size of nodes, you can define an array of node sizes, taking into account that
the default size is 300: size_list = [100 if G.nodes[n][ club ]== Mr. Hi else 300 “ ” ” ”
for n in G.nodes]
6. Pass the sizes using the node_size argument: nx.draw_spring(G, node_size =
size_list); plt.show()
7. The full list of arguments for NetworkX’s drawing functions can be consulted at
https://networkx.org/documentation/stable/reference/generated/networkx
.drawing.nx_pylab.draw_networkx.html
3.5. Other functions
1. Load disc.gml and try visualising it with the spring layout. When you show the drawing,
you should be able to see that the graph is disconnected.
2. Get the connected components of a graph: ccs = list(nx.connected_components(G))
3. Each connected component is represented as the list of IDs of its constituent nodes. To
obtain the induced subgraph based on a subset of nodes, pass the list of nodes into the
subgraph function, e.g.: cc_1 = G.subgraph(ccs[0])
4. Find and visualise the largest connected component of the graph (i.e., the entry of the result
of nx.connected_components(G) with the largest number of nodes in it).
5. You can write graphs to a separate GML file using the write_gml command, e.g.:
nx.write_gml(cc_1, disc_cc_1.gml ) “ ”
4. Homework
Task 1 (1 mark):
Using the provided graph files, create the following visualisations:
a. les_miserables.gml using yEd Organic layout
b. lastfm_sub.gml using Tulip FM3 layout
c. can_144.gml using NetworkX spring layout
Task 2 (1 mark):
Create a small graph (10-15 vertices) based on your friends network at uni/on your social network
of choice, where every person is a vertex and two people are connected with an edge if they know
each other (names can be obscured, e.g., only using initials). Use at least two different force directed layouts to visualise the graph.
Submission Instructions:
Compile all screenshots into one PDF file and submit the PDF file to Canvas.
Other file formats will not be accepted by the submission form.

发表回复