diff --git a/doc/source/vertex_edge.rst b/doc/source/vertex_edge.rst index c0078c1e..e85ace14 100644 --- a/doc/source/vertex_edge.rst +++ b/doc/source/vertex_edge.rst @@ -4,7 +4,7 @@ Vertices and Edges Vertex Types ------------- -A vertex can be of any Julia type. For example, it can be an integer, a character, or a string. In a simplistic setting where there is no additional information associated with a vertex, it is recommended to use ``Int`` as the vertex type, which would lead to the best performance. +A vertex can be of any Julia type. For example, it can be an integer, a character, or a string. This package provides two specific vertex types: ``KeyVertex`` and ``ExVertex``. The definition of ``KeyVertex`` is: @@ -40,14 +40,7 @@ The ``ExVertex`` type implements a ``vertex_index`` function, as returns the index of the vertex ``v``. -In addition, for integers, we have - -.. code-block:: python - - vertex_index(v::Integer) = v - -This makes it convenient to use integers as vertices in graphs. - +``SimpleGraph`` is a special case where the vertices are of type ``Int`` and store both their index and identity. In all other graphs, ``Int`` vertices are unordered indices. Edge Types ----------- diff --git a/src/adjacency_list.jl b/src/adjacency_list.jl index f71f0d79..f56c589f 100644 --- a/src/adjacency_list.jl +++ b/src/adjacency_list.jl @@ -35,7 +35,6 @@ is_directed(g::GenericAdjacencyList) = g.is_directed num_vertices(g::GenericAdjacencyList) = length(g.vertices) vertices(g::GenericAdjacencyList) = g.vertices -vertex_index{V<:ProvidedVertexType}(v::V, g::GenericAdjacencyList{V}) = vertex_index(v) num_edges(g::GenericAdjacencyList) = g.nedges diff --git a/src/common.jl b/src/common.jl index ebf1a840..cd3b0df4 100644 --- a/src/common.jl +++ b/src/common.jl @@ -8,8 +8,6 @@ typealias AttributeDict Dict{UTF8String, Any} # ################################################ -vertex_index(v::Integer) = v - immutable KeyVertex{K} index::Int key::K @@ -37,7 +35,10 @@ typealias ProvidedVertexType Union(Integer, KeyVertex, ExVertex) function vertex_index{V}(v::V, g::AbstractGraph{V}) @graph_requires g vertex_list - return vertex_index(v, vertices(g)) + if applicable(vertex_index, v) + return vertex_index(v) + end + return vertex_index(v, vertices(g)) # slow linear search end vertex_index(v, vs::AbstractArray) = findfirst(vs, v) diff --git a/src/edge_list.jl b/src/edge_list.jl index faf812cf..6f020950 100644 --- a/src/edge_list.jl +++ b/src/edge_list.jl @@ -26,7 +26,6 @@ is_directed(g::GenericEdgeList) = g.is_directed num_vertices(g::GenericEdgeList) = length(g.vertices) vertices(g::GenericEdgeList) = g.vertices -vertex_index{V<:ProvidedVertexType}(v::V, g::GenericEdgeList{V}) = vertex_index(v) num_edges(g::GenericEdgeList) = length(g.edges) edges(g::GenericEdgeList) = g.edges diff --git a/src/graph.jl b/src/graph.jl index 47fa6449..136f19cf 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -55,7 +55,8 @@ vertices(g::GenericGraph) = g.vertices num_edges(g::GenericGraph) = length(g.edges) edges(g::GenericGraph) = g.edges -vertex_index{V<:ProvidedVertexType}(v::V, g::GenericGraph{V}) = vertex_index(v) +vertex_index(v::Integer, g::SimpleGraph) = (v <= g.vertices[end]? v: 0) + edge_index{V,E}(e::E, g::GenericGraph{V,E}) = edge_index(e) out_edges{V}(v::V, g::GenericGraph{V}) = g.finclist[vertex_index(v, g)] @@ -69,6 +70,17 @@ in_neighbors{V}(v::V, g::GenericGraph{V}) = SourceIterator(g, in_edges(v, g)) # mutation +function add_vertex!(g::SimpleGraph) + # ensure SimpleGraph indices are consecutive, allowing O(1) indexing + v = g.vertices[end] + 1 + g.vertices = 1:v + push!(g.finclist, Int[]) + push!(g.binclist, Int[]) + v +end + +@deprecate add_vertex!(g::SimpleGraph,v) add_vertex!(g::SimpleGraph) + function add_vertex!{V}(g::GenericGraph{V}, v::V) push!(g.vertices, v) push!(g.finclist, Int[]) diff --git a/src/incidence_list.jl b/src/incidence_list.jl index 72eab6f8..15c84661 100644 --- a/src/incidence_list.jl +++ b/src/incidence_list.jl @@ -42,7 +42,6 @@ vertices(g::GenericIncidenceList) = g.vertices num_edges(g::GenericIncidenceList) = g.nedges -vertex_index{V<:ProvidedVertexType}(v::V, g::GenericIncidenceList{V}) = vertex_index(v) edge_index{V,E}(e::E, g::GenericIncidenceList{V,E}) = edge_index(e) out_edges{V}(v::V, g::GenericIncidenceList{V}) = g.inclist[vertex_index(v, g)] diff --git a/test/graph.jl b/test/graph.jl index cae32f5e..2042cd9a 100644 --- a/test/graph.jl +++ b/test/graph.jl @@ -10,7 +10,8 @@ using Base.Test # ################################################# -sgd = simple_graph(4) +sgd = simple_graph(3) +add_vertex!(sgd) # concept test