Skip to content

Commit

Permalink
Add wheel graph creation helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jciskey committed May 7, 2017
1 parent 97c5b77 commit 8c70f64
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions graph/predefined_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def build_cycle_graph(num_nodes):

return graph


def build_wheel_graph(num_nodes):
"""Builds a wheel graph with the specified number of nodes.
Ref: http://mathworld.wolfram.com/WheelGraph.html"""
# The easiest way to build a wheel graph is to build
# C_n-1 and then add a hub node and spoke edges
graph = build_cycle_graph(num_nodes - 1)

cycle_graph_vertices = graph.get_all_node_ids()

node_id = graph.new_node()
for cycle_node in cycle_graph_vertices:
graph.new_edge(node_id, cycle_node)

return graph

def build_triangle_graph():
"""Builds a triangle graph, C3.
Ref: http://mathworld.wolfram.com/CycleGraph.html"""
Expand Down Expand Up @@ -47,14 +63,9 @@ def build_diamond_graph():


def build_tetrahedral_graph():
"""Builds a tetrahedral graph.
"""Builds a tetrahedral graph, K4 (also, W4).
Ref: http://mathworld.wolfram.com/TetrahedralGraph.html"""
graph = build_triangle_graph()

graph.new_node()
graph.new_edge(1, 4)
graph.new_edge(2, 4)
graph.new_edge(3, 4)
graph = build_wheel_graph(4)

return graph

Expand Down

0 comments on commit 8c70f64

Please sign in to comment.