Graph Plotting Exercises
system:sage

{{{id=27|
# William -- if you use exercise 5, you might want to have them post the graph they're working on in a wiki or something so there aren't too many duplicates.  Or they can put it straight onto trac and I'll review them.
///
}}}

{{{id=0|
# Exercise 1:
# Set the vertex positions so that the graph
g = Graph({0:[2,3,4], 1:[2,3,4]})
# can be drawn with no edge crossings.
///
}}}

{{{id=1|
# Fast solution:
g.set_planar_positions()
g.show()
///
}}}

{{{id=6|
# Longer solution (or similar):
g.set_pos({0:[1,0], 1:[-1,0], 2:[0,1], 3:[0,0], 4:[0,-1]})
g.show()
///
}}}

{{{id=2|
# Exercise 2:
# Plot the digraph below with the vertices colored as follows:
# 1. If the vertex has both incoming and outgoing edges, make it orange.
# 2. If the vertex has only outgoing edges, make it yellow.
# 3. If the vertex has only incoming edges, make it red.
d = DiGraph({0:[1,2,3], 1:[0,2], 4:[0,1,2]})
///
}}}

{{{id=3|
# A solution:
d.show(vertex_colors={'orange':[0,1], 'yellow':[4], 'red':[2,3]})
///
}}}

{{{id=4|
# Exercise 3:
# Add the text "north", "south", "east" and "west" to a 2D plot of a graph with more than one vertex, so that the text is drawn above, below, to the right and to the left of the graph respectively.
///
}}}

{{{id=7|
# A solution:
# Note that the positions are hard-coded in the generators, so the text location in the following solution can be decided by either looking at the source of the Petersen graph constructor or by drawing a test plot with axes=True.
g = graphs.PetersenGraph()
G = g.plot() + text('north', [0,1.5]) \
    + text('south', [0,-1.5]) \
    + text('east', [1.5,0]) \
    + text('west', [-1.5,0])
G.show(axes=False, figsize=[4,4])
///
}}}

{{{id=13|
# Exercise 4:
# Draw an array of all graphs with 4 vertices.
///
}}}

{{{id=14|
# A solution:
graphs_list.show_graphs([i for i in graphs(4)])
///
}}}

{{{id=15|
# Exercise 4:
# Plot the Cayley Graph of the symmetric group S4.  Note that the edges of the Cayley graph have labels that directly represent the generators.  Use edge colorings in your plot to reflect these edge labels.
///
}}}

{{{id=18|
# A solution:
S4 = SymmetricGroup(4)
show(S4.cayley_graph(),color_by_label=True,vertex_size=5)
///
}}}

{{{id=22|
# Exercise 5:
# Contribute a generator of a named graph.  For example, execute the following cell.
///
}}}

{{{id=23|
graphs.PappusGraph??
///
}}}

{{{id=25|
# Exercise 5 continued:
# Your code should have a complete docstring and examples, as well as an "AUTHOR:" tag if you desire.  The preset position dictionary should represent a canonical drawing of the graph.  Ideas for more named graphs can be found by browsing the internet.  In particular, Wikipedia and Wolfram Mathworld have a few that may not already be included.
///
}}}

{{{id=26|

///
}}}