-
Notifications
You must be signed in to change notification settings - Fork 4
Creating Graphs
Ed Scheinerman edited this page Oct 3, 2022
·
14 revisions
Create a new graph with G = UndirectedGraph() or G = UG(). The vertices of this graph may be any Julia type.
WARNING: Problems may arise using vertices of Any type. It is better to specify
a specific type for the vertices.
A user may use G = UndirectedGraph{T}() to create a graph in which vertices are of type T. Two special cases are provided:
-
IntGraph()is equivalent toUndirectedGraph{Int}(). One may also haveG = IntGraph(n)which will create a graph whose vertex set is{1,2,...,n}(with no edges). -
UndirectedGraph(A)(whereAis ann-by-n, zero-one, hollow, symmetric matrix) creates a graph whose vertex set is{1,2,...,n}and whose adjacency matrix isA. -
UndirectedGraph()is equivalent toUndirectedGraph{String}(). -
StringGraph(file_name)builds a graph from data in a file.
Use H = deep_copy(G) to make a new graph H identical to G.
-
add!(G,v)adds a vertexvto the graph. -
add!(G,v,w)adds the edge(v,w)to the graph. -
add_edges!(G,edge_list)adds a list of edges to the graph;edge_listis aSetorArrayof 2-tuples. -
delete!(G,v)deletes vertexvfrom the graph. -
delete!(G,v,w)deletes the edge(v,w)from the graph.
Optionally, a graph can be given a name using the name function:
julia> G = IntGraph(10)
UndirectedGraph{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
UndirectedGraph{Int64} (n=10, m=1)