Skip to content

Creating Graphs

Ed Scheinerman edited this page Sep 19, 2018 · 14 revisions

Creating Graphs

A Blank Slate

Create a new graph with G = SimpleGraph(). The vertices of this graph may be any Julia type.

A user may use G = SimpleGraph{T}() to create a graph in which vertices are of type T. Two special cases are provided:

  • IntGraph() is equivalent to SimpleGraph{Int}(). One may also have G = IntGraph(n) which will create a graph whose vertex set is {1,2,...,n} (with no edges).
  • StringGraph() is equivalent to SimpleGraph{String}().

Adding/Deleting Vertices/Edges

  • add!(G,v) adds a vertex v to the graph.
  • add!(G,v,w) adds the edge (v,w) to the graph.
  • delete!(G,v) deletes vertex v from the graph.
  • delete!(G,v,w) deletes the edge (v,w) from the graph.

If A is an n-by-n symmetric, zero-one, hollow matrix, then SimpleGraph(A) creates a graph whose vertex set is {1,2,...,n} and whose adjacency matrix is A.

Graph Name

Optionally, a graph can be given a name using the name function:

julia> G = IntGraph(10)
SimpleGraph{Int64} (n=10, m=0)

julia> name(G,"Fred")

julia> G
Fred (n=10, m=0)

julia> name(G)
"Fred"

Note: If the graph is modified, then it's name is erased.

julia> add!(G,1,2)
true

julia> G
SimpleGraph{Int64} (n=10, m=1)
Clone this wiki locally