From 92b6f7659af9936426d8d42ebfc605d5bd759aee Mon Sep 17 00:00:00 2001 From: Sean Garborg Date: Mon, 27 Oct 2014 13:31:44 -0600 Subject: [PATCH 1/2] Consistent whitespace --- .travis.yml | 6 +-- README.md | 7 ++-- src/Graphs.jl | 3 +- src/adjacency_list.jl | 5 +-- src/bellmanford.jl | 8 ++-- src/breadth_first_visit.jl | 1 - src/concepts.jl | 11 +++--- src/connected_components.jl | 2 +- src/depth_first_visit.jl | 1 - src/dijkstra_spath.jl | 1 - src/edge_list.jl | 6 +-- src/floyd_warshall.jl | 49 ++++++++++++----------- src/gmatrix.jl | 32 +++++++-------- src/graph.jl | 11 +++--- src/graph_visit.jl | 1 - src/incidence_list.jl | 7 ++-- src/kruskal_mst.jl | 32 +++++++-------- src/layout.jl | 18 ++++----- src/prim_mst.jl | 1 - src/show.jl | 34 ++++++++-------- test/adjlist.jl | 6 +-- test/bellman_test.jl | 2 +- test/conn_comp.jl | 2 +- test/dfs.jl | 1 - test/dijkstra.jl | 8 +--- test/dot.jl | 4 +- test/edgelist.jl | 7 ++-- test/floyd.jl | 10 ++--- test/gmatrix.jl | 1 - test/graph.jl | 4 +- test/graph_bench.jl | 10 ++--- test/inclist.jl | 77 ++++++++++++++++++------------------- test/mst.jl | 4 +- test/random.jl | 40 +++++++++---------- 34 files changed, 192 insertions(+), 220 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7df96f57..4ec3c830 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,11 @@ language: cpp -compiler: +compiler: - gcc notifications: email: false env: - - JULIAVERSION="juliareleases" - - JULIAVERSION="julianightlies" + - JULIAVERSION="juliareleases" + - JULIAVERSION="julianightlies" before_install: - sudo add-apt-repository ppa:staticfloat/julia-deps -y - sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y diff --git a/README.md b/README.md index 8a58afc2..07717ba0 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ Graphs.jl is a Julia package that provides graph types and algorithms. The desig ### Main Features -An important aspect of *Graphs.jl* is the generic abstraction of graph concepts expressed via standardized interfaces, which allows access to a graph's structure while hiding the implementation details. This encourages reuse of data structures and algorithms. In particular, one can write generic graph algorithms that can be applied to different graph types as long as they implement the required interface. +An important aspect of *Graphs.jl* is the generic abstraction of graph concepts expressed via standardized interfaces, which allows access to a graph's structure while hiding the implementation details. This encourages reuse of data structures and algorithms. In particular, one can write generic graph algorithms that can be applied to different graph types as long as they implement the required interface. In addition to the generic abstraction, there are other important features: -* A variety of graph types tailored to different purposes +* A variety of graph types tailored to different purposes - generic adjacency list - generic incidence list - a simple graph type with compact and efficient representation @@ -29,7 +29,7 @@ In addition to the generic abstraction, there are other important features: - random graph generation: Erdős–Rényi, Watts-Strogatz (see the RandomGraphs.jl package for more random graph models) - more algorithms are being implemented - + * Matrix-based characterization: adjacency matrix, weight matrix, Laplacian matrix * All data structures and algorithms are implemented in *pure Julia*, and thus they are portable. @@ -40,4 +40,3 @@ In addition to the generic abstraction, there are other important features: ### Documentation Please refer to [*Graph.jl Documentation*](http://graphsjl-docs.readthedocs.org/en/latest/) for latest documentation. - diff --git a/src/Graphs.jl b/src/Graphs.jl index 3be685ff..76935fba 100644 --- a/src/Graphs.jl +++ b/src/Graphs.jl @@ -80,7 +80,7 @@ module Graphs dijkstra_shortest_paths_withlog, # bellmanford - BellmanFordStates, create_bellman_ford_states, NegativeCycleError, + BellmanFordStates, create_bellman_ford_states, NegativeCycleError, bellman_ford_shortest_paths!, bellman_ford_shortest_paths, has_negative_edge_cycle, @@ -130,4 +130,3 @@ module Graphs include("random.jl") end - diff --git a/src/adjacency_list.jl b/src/adjacency_list.jl index d6e7de29..f71f0d79 100644 --- a/src/adjacency_list.jl +++ b/src/adjacency_list.jl @@ -24,7 +24,7 @@ typealias AdjacencyList{V} GenericAdjacencyList{V, Vector{V}, Vector{Vector{V}}} ## construction -simple_adjlist(nv::Int; is_directed::Bool=true) = SimpleAdjacencyList(is_directed, 1:nv, 0, multivecs(Int, nv)) +simple_adjlist(nv::Int; is_directed::Bool=true) = SimpleAdjacencyList(is_directed, 1:nv, 0, multivecs(Int, nv)) adjlist{V}(vs::Vector{V}; is_directed::Bool=true) = AdjacencyList{V}(is_directed, vs, 0, Vector{V}[]) adjlist{V}(::Type{V}; is_directed::Bool=true) = adjlist(V[]; is_directed=is_directed) @@ -53,7 +53,7 @@ end add_vertex!(g::GenericAdjacencyList, x) = add_vertex!(g, make_vertex(g, x)) function add_edge!{V}(g::GenericAdjacencyList{V}, u::V, v::V) - nv::Int = num_vertices(g) + nv::Int = num_vertices(g) iu = vertex_index(u, g)::Int push!(g.adjlist[iu], v) g.nedges += 1 @@ -100,4 +100,3 @@ function simple_adjlist{T<:Number}(A::AbstractMatrix{T}; is_directed::Bool=true) end return SimpleAdjacencyList(is_directed, 1:n, m, alist) end - diff --git a/src/bellmanford.jl b/src/bellmanford.jl index b1f21065..2a414ae1 100644 --- a/src/bellmanford.jl +++ b/src/bellmanford.jl @@ -19,13 +19,13 @@ function create_bellman_ford_states{V,D<:Number}(g::AbstractGraph{V}, ::Type{D}) n = num_vertices(g) parents = Array(V, n) dists = fill(typemax(D), n) - - BellmanFordStates(parents, dists) + + BellmanFordStates(parents, dists) end function bellman_ford_shortest_paths!{V,D}( graph::AbstractGraph{V}, - edge_dists::AbstractEdgePropertyInspector{D}, + edge_dists::AbstractEdgePropertyInspector{D}, sources::AbstractVector{V}, state::BellmanFordStates{V,D}) @@ -72,7 +72,7 @@ end function bellman_ford_shortest_paths{V,D}( graph::AbstractGraph{V}, - edge_dists::AbstractEdgePropertyInspector{D}, + edge_dists::AbstractEdgePropertyInspector{D}, sources::AbstractVector{V}) state = create_bellman_ford_states(graph, D) bellman_ford_shortest_paths!(graph, edge_dists, sources, state) diff --git a/src/breadth_first_visit.jl b/src/breadth_first_visit.jl index e04b74df..f988baef 100644 --- a/src/breadth_first_visit.jl +++ b/src/breadth_first_visit.jl @@ -128,4 +128,3 @@ function gdistances(graph::AbstractGraph, sources; defaultdist::Int=-1) dists = fill(defaultdist, num_vertices(graph)) gdistances!(graph, sources, dists) end - diff --git a/src/concepts.jl b/src/concepts.jl index 4882b390..d7b10ea3 100644 --- a/src/concepts.jl +++ b/src/concepts.jl @@ -13,13 +13,13 @@ edge_type{V,E}(g::AbstractGraph{V,E}) = E ### concepts const graph_concept_symbols = Set([ - :vertex_list, - :edge_list, - :vertex_map, + :vertex_list, + :edge_list, + :vertex_map, :edge_map, - :adjacency_list, + :adjacency_list, :incidence_list, - :bidirectional_adjacency_list, + :bidirectional_adjacency_list, :bidirectional_incidence_list, :adjacency_matrix]) @@ -76,4 +76,3 @@ end macro graph_requires(g, concepts...) esc(_graph_requires_code(g, concepts...)) end - diff --git a/src/connected_components.jl b/src/connected_components.jl index f5c9379c..1d96e574 100644 --- a/src/connected_components.jl +++ b/src/connected_components.jl @@ -97,7 +97,7 @@ type TarjanVisitor{G<:AbstractGraph,V} <: AbstractGraphVisitor components::Vector{Vector{V}} end -TarjanVisitor{V}(graph::AbstractGraph{V}) = TarjanVisitor{typeof(graph),V}(graph, +TarjanVisitor{V}(graph::AbstractGraph{V}) = TarjanVisitor{typeof(graph),V}(graph, V[], Int[], zeros(Int, num_vertices(graph)), Vector{V}[]) function discover_vertex!(vis::TarjanVisitor, v) diff --git a/src/depth_first_visit.jl b/src/depth_first_visit.jl index 762326e4..3eb45c4b 100644 --- a/src/depth_first_visit.jl +++ b/src/depth_first_visit.jl @@ -159,4 +159,3 @@ function topological_sort_by_dfs{V}(graph::AbstractGraph{V}) reverse(visitor.vertices) end - diff --git a/src/dijkstra_spath.jl b/src/dijkstra_spath.jl index ad3c2cd5..b40de388 100644 --- a/src/dijkstra_spath.jl +++ b/src/dijkstra_spath.jl @@ -243,4 +243,3 @@ function dijkstra_shortest_paths_withlog{V,D}( graph::AbstractGraph{V}, edge_dists::Vector{D}, sources::AbstractVector{V}) dijkstra_shortest_paths(graph, edge_dists, sources, visitor=LogDijkstraVisitor(STDOUT)) end - diff --git a/src/edge_list.jl b/src/edge_list.jl index 01713e02..faf812cf 100644 --- a/src/edge_list.jl +++ b/src/edge_list.jl @@ -13,10 +13,10 @@ typealias EdgeList{V,E} GenericEdgeList{V,E,Vector{V},Vector{E}} # construction -simple_edgelist{E}(nv::Integer, edges::Vector{E}; is_directed::Bool=true) = +simple_edgelist{E}(nv::Integer, edges::Vector{E}; is_directed::Bool=true) = SimpleEdgeList{E}(is_directed, 1:int(nv), edges) -edgelist{V,E}(vertices::Vector{V}, edges::Vector{E}; is_directed::Bool=true) = +edgelist{V,E}(vertices::Vector{V}, edges::Vector{E}; is_directed::Bool=true) = EdgeList{V,E}(is_directed, vertices, edges) @@ -39,5 +39,3 @@ add_vertex!{V}(g::GenericEdgeList{V}, x) = add_vertex!(g, make_vertex(g, x)) add_edge!{V,E}(g::GenericEdgeList{V,E}, e::E) = (push!(g.edges, e); e) add_edge!{V,E}(g::GenericEdgeList{V,E}, u::V, v::V) = add_edge!(g, make_edge(g, u, v)) - - diff --git a/src/floyd_warshall.jl b/src/floyd_warshall.jl index 02b6553a..fd58a4d6 100644 --- a/src/floyd_warshall.jl +++ b/src/floyd_warshall.jl @@ -1,72 +1,71 @@ # Floyd Warshall algorithm to find shortest paths between all pairs of vertices -function floyd_warshall!{W}(dists::AbstractMatrix{W}) # dists: minimum distance matrix (initialized to edge distances) - +function floyd_warshall!{W}(dists::AbstractMatrix{W}) # dists: minimum distance matrix (initialized to edge distances) + # argument checking - + n = size(dists, 1) if size(dists, 2) != n throw(ArgumentError("dists should be a square matrix.")) end - + # initialize - + for i = 1 : n dists[i,i] = 0 end - + # main loop - + for k = 1 : n, i = 1 : n, j = 1 : n d = dists[i,k] + dists[k,j] if d < dists[i,j] dists[i,j] = d - end - end - - dists + end + end + + dists end function floyd_warshall!{W}( - dists::AbstractMatrix{W}, # minimum distance matrix (initialized to edge distances) + dists::AbstractMatrix{W}, # minimum distance matrix (initialized to edge distances) nexts::AbstractMatrix{Int}) # nexts(i,j) = the next hop from i when traveling from i to j via shortest path - + # argument checking - + n = size(dists, 1) if size(dists, 2) != n throw(ArgumentError("dists should be a square matrix.")) end - + if size(nexts) != (n, n) throw(ArgumentError("nexts should be an n-by-n matrix.")) end - + # initialize - + for i = 1 : n dists[i,i] = 0 end - + for j = 1 : n, i = 1 : n nexts[i,j] = isfinite(dists[i,j]) ? j : 0 end - + # main loop - + for k = 1 : n, i = 1 : n, j = 1 : n d = dists[i,k] + dists[k,j] if d < dists[i,j] dists[i,j] = d nexts[i,j] = nexts[i,k] - end - end - - dists + end + end + + dists end floyd_warshall(weights::AbstractMatrix) = floyd_warshall!(copy(weights)) - diff --git a/src/gmatrix.jl b/src/gmatrix.jl index be32f218..f14aaa0c 100644 --- a/src/gmatrix.jl +++ b/src/gmatrix.jl @@ -35,7 +35,7 @@ function matrix_from_adjpairs!(a::AbstractMatrix, g::AbstractGraph, gen) vi = vertex_index(v, g) val = get(gen, g, u, v) a[ui, vi] = val - end + end end else error("g does not implement required interface.") @@ -44,7 +44,7 @@ function matrix_from_adjpairs!(a::AbstractMatrix, g::AbstractGraph, gen) return a end -matrix_from_adjpairs(g::AbstractGraph, gen) = +matrix_from_adjpairs(g::AbstractGraph, gen) = (n = num_vertices(g); matrix_from_adjpairs!(zeros(eltype(gen), n, n), g, gen)) @@ -80,7 +80,7 @@ function sparse_matrix_from_adjpairs(g::AbstractGraph, gen) ui = vertex_index(u, g) vi = vertex_index(v, g) val = get(gen, g, u, v) - + idx += 1 I[idx] = ui J[idx] = vi @@ -123,7 +123,7 @@ function matrix_from_edges!(a::AbstractMatrix, g::AbstractGraph, gen) v = target(e, g) ui = vertex_index(u, g) vi = vertex_index(v, g) - val = get(gen, g, e) + val = get(gen, g, e) a[ui, vi] = val end else @@ -155,7 +155,7 @@ function matrix_from_edges!(a::AbstractMatrix, g::AbstractGraph, gen) return a end -matrix_from_edges(g::AbstractGraph, gen) = +matrix_from_edges(g::AbstractGraph, gen) = (n = num_vertices(g); matrix_from_edges!(zeros(eltype(gen), n, n), g, gen)) function sparse_matrix_from_edges(g::AbstractGraph, gen) @@ -190,7 +190,7 @@ function sparse_matrix_from_edges(g::AbstractGraph, gen) ui = vertex_index(u, g) vi = vertex_index(v, g) val = get(gen, g, e) - + idx += 1 I[idx] = ui J[idx] = vi @@ -210,7 +210,7 @@ function sparse_matrix_from_edges(g::AbstractGraph, gen) v = target(e, g) vi = vertex_index(v, g) val = get(gen, g, e) - + idx += 1 I[idx] = ui J[idx] = vi @@ -245,7 +245,7 @@ adjacency_matrix_sparse{T<:Number}(g::AbstractGraph, ::Type{T}) = sparse_matrix_ adjacency_matrix_sparse(g::AbstractGraph) = adjacency_matrix_sparse(g, Bool) ### weight matrix - + type _GenEdgeWeight{Weights} weights::Weights end @@ -268,10 +268,10 @@ function init_distancemat{T<:Number}(n::Int, dinf::T) return a end -distance_matrix(g::AbstractGraph, dists::AbstractVector, dinf) = +distance_matrix(g::AbstractGraph, dists::AbstractVector, dinf) = (n = num_vertices(g); matrix_from_edges!(init_distancemat(n, dinf), g, _GenEdgeWeight(dists))) -distance_matrix{T<:Real}(g::AbstractGraph, dists::AbstractVector{T}) = +distance_matrix{T<:Real}(g::AbstractGraph, dists::AbstractVector{T}) = distance_matrix(g, dists, typemax(T)) @@ -388,7 +388,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, ::Type{T}) vi = vertex_index(v, g) if ui != vi idx += 1 - I[idx] = ui + I[idx] = ui J[idx] = vi vals[idx] = -1 @@ -408,7 +408,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, ::Type{T}) vi = vertex_index(v, g) if ui < vi idx += 1 - I[idx] = ui + I[idx] = ui J[idx] = vi vals[idx] = -1 @@ -425,7 +425,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, ::Type{T}) else error("g does not implement proper interface.") end - + for i = 1:n di = degs[i] if isnz(di) @@ -469,7 +469,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, eweights::Abstract if ui != vi wi = eweights[edge_index(e, g)] idx += 1 - I[idx] = ui + I[idx] = ui J[idx] = vi vals[idx] = -wi @@ -491,7 +491,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, eweights::Abstract if ui < vi wi = eweights[edge_index(e, g)] idx += 1 - I[idx] = ui + I[idx] = ui J[idx] = vi vals[idx] = -wi @@ -508,7 +508,7 @@ function laplacian_matrix_sparse{T<:Number}(g::AbstractGraph, eweights::Abstract else error("g does not implement proper interface.") end - + for i = 1:n di = degs[i] if isnz(di) diff --git a/src/graph.jl b/src/graph.jl index 1ac47252..47fa6449 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -8,10 +8,10 @@ type GenericGraph{V,E,VList,EList,IncList} <: AbstractGraph{V,E} vertices::VList # an indexable container of vertices edges::EList # an indexable container of edges finclist::IncList # forward incidence list - binclist::IncList # backward incidence list + binclist::IncList # backward incidence list end -@graph_implements GenericGraph vertex_list edge_list vertex_map edge_map +@graph_implements GenericGraph vertex_list edge_list vertex_map edge_map @graph_implements GenericGraph bidirectional_adjacency_list bidirectional_incidence_list # SimpleGraph: @@ -28,10 +28,10 @@ typealias Graph{V,E} GenericGraph{V,E,Vector{V},Vector{E},Vector{Vector{E}}} # construction -simple_graph(n::Integer; is_directed::Bool=true) = - SimpleGraph(is_directed, +simple_graph(n::Integer; is_directed::Bool=true) = + SimpleGraph(is_directed, 1:int(n), # vertices - IEdge[], # edges + IEdge[], # edges multivecs(IEdge, n), # finclist multivecs(IEdge, n)) # binclist @@ -96,4 +96,3 @@ end add_edge!{V,E}(g::GenericGraph{V,E}, e::E) = add_edge!(g, source(e, g), target(e, g), e) add_edge!{V,E}(g::GenericGraph{V,E}, u::V, v::V) = add_edge!(g, u, v, make_edge(g, u, v)) - diff --git a/src/graph_visit.jl b/src/graph_visit.jl index ca8a13a2..dad88b9f 100644 --- a/src/graph_visit.jl +++ b/src/graph_visit.jl @@ -92,4 +92,3 @@ end traverse_graph_withlog(g::AbstractGraph, alg::AbstractGraphVisitAlgorithm, sources) = traverse_graph_withlog(g, alg, sources, STDOUT) - diff --git a/src/incidence_list.jl b/src/incidence_list.jl index 1a66dd60..72eab6f8 100644 --- a/src/incidence_list.jl +++ b/src/incidence_list.jl @@ -10,7 +10,7 @@ ########################################################### type GenericIncidenceList{V, E, VList, IncList} <: AbstractGraph{V, E} - is_directed::Bool + is_directed::Bool vertices::VList nedges::Int inclist::IncList @@ -23,10 +23,10 @@ typealias IncidenceList{V,E} GenericIncidenceList{V, E, Vector{V}, Vector{Vector # construction -simple_inclist(nv::Integer; is_directed::Bool=true) = +simple_inclist(nv::Integer; is_directed::Bool=true) = SimpleIncidenceList(is_directed, 1:int(nv), 0, multivecs(IEdge, nv)) -inclist{V,E}(vs::Vector{V}, ::Type{E}; is_directed::Bool = true) = +inclist{V,E}(vs::Vector{V}, ::Type{E}; is_directed::Bool = true) = IncidenceList{V,E}(is_directed, vs, 0, multivecs(E, length(vs))) inclist{V,E}(::Type{V}, ::Type{E}; is_directed::Bool = true) = inclist(V[], E; is_directed=is_directed) @@ -73,4 +73,3 @@ end add_edge!{V,E}(g::GenericIncidenceList{V,E}, e::E) = add_edge!(g, source(e, g), target(e, g), e) add_edge!{V,E}(g::GenericIncidenceList{V, E}, u::V, v::V) = add_edge!(g, u, v, make_edge(g, u, v)) - diff --git a/src/kruskal_mst.jl b/src/kruskal_mst.jl index 8a63b348..ff12fcbe 100644 --- a/src/kruskal_mst.jl +++ b/src/kruskal_mst.jl @@ -3,53 +3,53 @@ # select edges from a sorted list of weighted edges function kruskal_select{V,E,W}( - graph::AbstractGraph{V,E}, + graph::AbstractGraph{V,E}, sorted_wedges::AbstractVector{WeightedEdge{E,W}}, K::Integer) - - @graph_requires graph vertex_map - + + @graph_requires graph vertex_map + n = num_vertices(graph) re = Array(E, 0) rw = Array(W, 0) - + if n > 1 dsets = IntDisjointSets(n) sizehint(re, n-1) sizehint(rw, n-1) - + ui::Int = 0 vi::Int = 0 - + for we in sorted_wedges - e::E = we.edge + e::E = we.edge ui = vertex_index(source(e, graph), graph) vi = vertex_index(target(e, graph), graph) - + if !in_same_set(dsets, ui, vi) union!(dsets, ui, vi) push!(re, e) push!(rw, we.weight) end - + if num_groups(dsets) <= K break - end + end end end - + return (re, rw) end function kruskal_minimum_spantree(graph::AbstractGraph, eweights::AbstractEdgePropertyInspector; K::Integer=1) - + # collect & sort edges - + wedges = collect_weighted_edges(graph, eweights) sort!(wedges) - + # select the tree edges - kruskal_select(graph, wedges, K) + kruskal_select(graph, wedges, K) end diff --git a/src/layout.jl b/src/layout.jl index da2fd03b..18678d93 100644 --- a/src/layout.jl +++ b/src/layout.jl @@ -3,7 +3,7 @@ function layout_random(g::GenericGraph, dim=2) # Generate random locations in unit-square. - # + # # Each node is associated with a dim-dimensional random vector in # [0,1]^{dim}. # @@ -13,10 +13,10 @@ function layout_random(g::GenericGraph, dim=2) # Returns: Dict keyed by vertices with positions as values. # # Ported from networkx. - + n = num_vertices(g) pos = rand(n, dim) - + return Dict(g.vertices, pos) end @@ -45,9 +45,9 @@ function _spectral_sparse{T<:Real}(L::CSCMatrix{T}, dim=2) end function layout_spectral(g::GenericGraph, dim=2, scale=1) - # Use eigenvectors of graph Laplacian as coordinates of + # Use eigenvectors of graph Laplacian as coordinates of # nodes. - # + # # g : graph to embed # dim : number of dimensions to use # @@ -57,16 +57,16 @@ function layout_spectral(g::GenericGraph, dim=2, scale=1) # * Sparse eigen-solver used for large graphs. # # Based on networkx version. - + error("Spectral not done yet") end function layout_spectral{W}(g::GenericGraph, eweights::AbstractVector{W}, dim=2, scale=1) - # Use eigenvectors of weighted graph Laplacian as coordinates of + # Use eigenvectors of weighted graph Laplacian as coordinates of # nodes. - # + # # g : graph to embed # dim : number of dimensions to use # @@ -76,7 +76,7 @@ function layout_spectral{W}(g::GenericGraph, eweights::AbstractVector{W}, # * Sparse eigen-solver used for large graphs. # # Based on networkx version. - + error("Weighted spectral not done yet") end diff --git a/src/prim_mst.jl b/src/prim_mst.jl index 5d68b284..7a949fa0 100644 --- a/src/prim_mst.jl +++ b/src/prim_mst.jl @@ -239,4 +239,3 @@ function prim_minimum_spantree_withlog{V,E,W}( edge_weights = VectorEdgePropertyInspector(edge_weight_vec) prim_minimum_spantree!(graph, edge_weights, root, visitor, state) end - diff --git a/src/show.jl b/src/show.jl index ba91868f..3694f308 100644 --- a/src/show.jl +++ b/src/show.jl @@ -12,50 +12,50 @@ function show(io::IO, e::Union(Edge, ExEdge)) print(io, "edge [$(e.index)]: $(e.source) -- $(e.target)") end -function show(io::IO, graph::AbstractGraph) - title = is_directed(graph) ? "Directed Graph" : "Undirected Graph" +function show(io::IO, graph::AbstractGraph) + title = is_directed(graph) ? "Directed Graph" : "Undirected Graph" print(io, "$title ($(num_vertices(graph)) vertices, $(num_edges(graph)) edges)") end -function show_details(io::IO, graph::AbstractGraph) - title = is_directed(graph) ? "Directed Graph" : "Undirected Graph" +function show_details(io::IO, graph::AbstractGraph) + title = is_directed(graph) ? "Directed Graph" : "Undirected Graph" println(io, "$title with $(num_vertices(graph)) vertices and $(num_edges(graph)) edges:") - + if !implements_vertex_list(graph) return end - - if implements_incidence_list(graph) - println(io, "Incidence List:") + + if implements_incidence_list(graph) + println(io, "Incidence List:") for v in vertices(graph) println(io, "$(v): ") for e in out_edges(v, graph) print(io, " ") - println(io, e) - end + println(io, e) + end end - + elseif implements_adjacency_list(graph) println(io, "Adjacency List:") for v in vertices(graph) println(io, "$(v): ") for u in out_neighbors(v, graph) print(io, " ") - println(io, "$v -- $u") - end + println(io, "$v -- $u") + end end - + else println(io, "Vertices:") for v in vertices(graph) println(io, "$(v)") end - + if implements_edge_list(graph) println(io, "Edges:") for e in edges(graph) - println(io, "$(e)") - end + println(io, "$(e)") + end end end end diff --git a/test/adjlist.jl b/test/adjlist.jl index 3390b7e2..16c668a7 100644 --- a/test/adjlist.jl +++ b/test/adjlist.jl @@ -70,10 +70,10 @@ end for g in [adjlist(KeyVertex{ASCIIString}), adjlist(ASCIIString)] - vs = [ add_vertex!(g, "a"), - add_vertex!(g, "b"), + vs = [ add_vertex!(g, "a"), + add_vertex!(g, "b"), add_vertex!(g, "c") ] - + @test num_vertices(g) == 3 for i = 1 : 3 diff --git a/test/bellman_test.jl b/test/bellman_test.jl index d0eaf5de..0f6b82ca 100644 --- a/test/bellman_test.jl +++ b/test/bellman_test.jl @@ -97,6 +97,6 @@ end s3 = bellman_ford_shortest_paths(g3, eweights3, [5]) -@test s3.parents == [2, 4, 1, 5, 5] +@test s3.parents == [2, 4, 1, 5, 5] @test s3.dists == [2.0, 4.0, -2.0, 7.0, 0.0] @test !has_negative_edge_cycle(g3, eweights1) diff --git a/test/conn_comp.jl b/test/conn_comp.jl index 8361fa5a..cb5b5203 100644 --- a/test/conn_comp.jl +++ b/test/conn_comp.jl @@ -42,7 +42,7 @@ scc = strongly_connected_components(g) # test 2, from Vazirani's notes # (http://www.cs.berkeley.edu/~vazirani/s99cs170/notes/lec12.pdf) -eds = [(1, 2), (2, 3), (2, 4), (2, 5), (3, 6), +eds = [(1, 2), (2, 3), (2, 4), (2, 5), (3, 6), (4, 5), (4, 7), (5, 2), (5, 6), (5, 7), (6, 3), (6, 8), (7, 8), (7, 10), (8, 7), (9, 7), (10, 9), (10, 11), (11, 12), (12, 10)] diff --git a/test/dfs.jl b/test/dfs.jl index f4fc237e..996402eb 100644 --- a/test/dfs.jl +++ b/test/dfs.jl @@ -74,4 +74,3 @@ for tset in testsets end end end - diff --git a/test/dijkstra.jl b/test/dijkstra.jl index e99c2a0c..afea9931 100644 --- a/test/dijkstra.jl +++ b/test/dijkstra.jl @@ -72,7 +72,7 @@ s1b = dijkstra_shortest_paths(g1, eweights1, [1], visitor=EndWhenNode(5)) @test s1b.parents == [1, 3, 1, 3, 3] @test s1b.dists == [0.0, 8.0, 5.0, 14.0, 7.0] -@test s1b.colormap == [2, 1, 2, 1, 2] +@test s1b.colormap == [2, 1, 2, 1, 2] # g2: the example in Wikipedia g2 = simple_inclist(6, is_directed=false) @@ -95,7 +95,7 @@ for i = 1 : ne add_edge!(g2, we[1], we[2]) eweights2[i] = we[3] end - + @assert num_vertices(g2) == 6 @assert num_edges(g2) == 9 @@ -110,7 +110,3 @@ s2 = dijkstra_shortest_paths(g2, eweights2, [1]) @test s2.parents == [1, 1, 1, 3, 6, 3] @test s2.dists == [0., 7., 9., 20., 20., 11.] @test s2.colormap == [2, 2, 2, 2, 2, 2] - - - - diff --git a/test/dot.jl b/test/dot.jl index 2dd71efe..c4b460ff 100644 --- a/test/dot.jl +++ b/test/dot.jl @@ -2,11 +2,11 @@ using Graphs using Base.Test function has_match(regex, str) - !isa(match(regex, str), Nothing) + !isa(match(regex, str), Nothing) end function xor(a, b) - (a || b) && !(a && b) + (a || b) && !(a && b) end # Vertex attributes get layed out correctly diff --git a/test/edgelist.jl b/test/edgelist.jl index 1ab0aefb..e3e9d9ad 100644 --- a/test/edgelist.jl +++ b/test/edgelist.jl @@ -52,11 +52,11 @@ gu = simple_edgelist(5, eds; is_directed=false) for T in [ ExVertex, ASCIIString ] g = edgelist(T[], ExEdge{T}[]) - vs = [ add_vertex!(g, "a"), - add_vertex!(g, "b"), + vs = [ add_vertex!(g, "a"), + add_vertex!(g, "b"), add_vertex!(g, "c") ] - es = [ add_edge!(g, vs[1], vs[2]), + es = [ add_edge!(g, vs[1], vs[2]), add_edge!(g, vs[1], vs[3]) ] @test vertex_type(g) == T @@ -69,4 +69,3 @@ for T in [ ExVertex, ASCIIString ] @test num_edges(g) == 2 @test edges(g) == es end - diff --git a/test/floyd.jl b/test/floyd.jl index 1bc64882..8a377400 100644 --- a/test/floyd.jl +++ b/test/floyd.jl @@ -7,14 +7,14 @@ eweights = [ 0. 1. 5. Inf; Inf 0. 3. 8.; Inf 3. 0. 2.; 6. Inf Inf 0. ] - -dists0 = [ 0. 1. 4. 6.; + +dists0 = [ 0. 1. 4. 6.; 11. 0. 3. 5.; 8. 3. 0. 2.; 6. 7. 10. 0. ] nexts0 = [1 2 2 2; 3 2 3 3; 4 2 3 4; 1 1 1 4] - + dists = copy(eweights) nexts = fill(-1, (4, 4)) @@ -24,7 +24,3 @@ floyd_warshall!(dists, nexts) @test nexts == nexts0 @test floyd_warshall(eweights) == dists0 - - - - diff --git a/test/gmatrix.jl b/test/gmatrix.jl index d93e7422..d5125758 100644 --- a/test/gmatrix.jl +++ b/test/gmatrix.jl @@ -98,4 +98,3 @@ Lw_s = sparse(Lw) @test laplacian_matrix_sparse(gu_elst, eweights) == Lw_s @test laplacian_matrix_sparse(gu_ilst, eweights) == Lw_s - diff --git a/test/graph.jl b/test/graph.jl index 2c09bfab..cae32f5e 100644 --- a/test/graph.jl +++ b/test/graph.jl @@ -49,7 +49,7 @@ es = [ add_edge!(sgd, 1, 2) @test num_edges(sgd) == 4 -# outgoing +# outgoing @test [out_degree(v, sgd) for v = 1:4] == [2, 1, 1, 0] @@ -100,7 +100,7 @@ end add_edge!(sgu, 1, 2) add_edge!(sgu, 1, 3) add_edge!(sgu, 2, 4) -add_edge!(sgu, 3, 4) +add_edge!(sgu, 3, 4) add_edge!(sgu, 4, 1) es = sgu.edges diff --git a/test/graph_bench.jl b/test/graph_bench.jl index 461b4a93..0200bc49 100644 --- a/test/graph_bench.jl +++ b/test/graph_bench.jl @@ -13,16 +13,16 @@ g_gra = simple_graph(nv) shuff = [1:nv] -for u = 1 : nv +for u = 1 : nv for j in 1 : deg - + v = u - + while v == u k = rand(j+1:nv) shuff[j], shuff[k] = shuff[k], shuff[j] v = shuff[j] - end + end add_edge!(g_adj, u, v) add_edge!(g_inc, u, v) add_edge!(g_gra, u, v) @@ -101,5 +101,3 @@ println("Benchmark of depth-first traversal") println("Benchmark of Dijkstra shortest paths") @graph_bench run_dijkstra SimpleIncidenceList g_inc 5 @graph_bench run_dijkstra SimpleGraph g_gra 5 - - diff --git a/test/inclist.jl b/test/inclist.jl index b14bdeaf..af0ec08e 100644 --- a/test/inclist.jl +++ b/test/inclist.jl @@ -132,61 +132,60 @@ add_edge!(gu, 4, 5) # ################################################# let - for g in [inclist(KeyVertex{ASCIIString}), inclist(ASCIIString)] + for g in [inclist(KeyVertex{ASCIIString}), inclist(ASCIIString)] - vs = [ add_vertex!(g, "a"), add_vertex!(g, "b"), add_vertex!(g, "c") ] + vs = [ add_vertex!(g, "a"), add_vertex!(g, "b"), add_vertex!(g, "c") ] - @test num_vertices(g) == 3 + @test num_vertices(g) == 3 - for i = 1 : 3 - @test vertices(g)[i] == vs[i] - @test out_degree(vs[i], g) == 0 - end + for i = 1 : 3 + @test vertices(g)[i] == vs[i] + @test out_degree(vs[i], g) == 0 + end - add_edge!(g, vs[1], vs[2]) - add_edge!(g, vs[1], vs[3]) - add_edge!(g, vs[2], vs[3]) + add_edge!(g, vs[1], vs[2]) + add_edge!(g, vs[1], vs[3]) + add_edge!(g, vs[2], vs[3]) - @test out_degree(vs[1], g) == 2 - @test out_degree(vs[2], g) == 1 - @test out_degree(vs[3], g) == 0 + @test out_degree(vs[1], g) == 2 + @test out_degree(vs[2], g) == 1 + @test out_degree(vs[3], g) == 0 - @test out_edges(vs[1], g) == [Edge(1, vs[1], vs[2]), Edge(2, vs[1], vs[3])] - @test out_edges(vs[2], g) == [Edge(3, vs[2], vs[3])] - @test isempty(out_edges(vs[3], g)) + @test out_edges(vs[1], g) == [Edge(1, vs[1], vs[2]), Edge(2, vs[1], vs[3])] + @test out_edges(vs[2], g) == [Edge(3, vs[2], vs[3])] + @test isempty(out_edges(vs[3], g)) end end let - g = inclist(ExVertex, ExEdge{ExVertex}; is_directed=false) + g = inclist(ExVertex, ExEdge{ExVertex}; is_directed=false) - vs = [ add_vertex!(g, ExVertex(1,"a")), - add_vertex!(g, ExVertex(2,"b")), - add_vertex!(g, ExVertex(3,"c")) ] + vs = [ add_vertex!(g, ExVertex(1,"a")), + add_vertex!(g, ExVertex(2,"b")), + add_vertex!(g, ExVertex(3,"c")) ] - @test num_vertices(g) == 3 + @test num_vertices(g) == 3 - for i = 1 : 3 - @test vertices(g)[i] == vs[i] - @test out_degree(vs[i], g) == 0 - end + for i = 1 : 3 + @test vertices(g)[i] == vs[i] + @test out_degree(vs[i], g) == 0 + end - add_edge!(g, vs[1], vs[2]) - add_edge!(g, vs[1], vs[3]) - add_edge!(g, vs[2], vs[3]) + add_edge!(g, vs[1], vs[2]) + add_edge!(g, vs[1], vs[3]) + add_edge!(g, vs[2], vs[3]) - @test out_degree(vs[1], g) == 2 - @test out_degree(vs[2], g) == 2 - @test out_degree(vs[3], g) == 2 + @test out_degree(vs[1], g) == 2 + @test out_degree(vs[2], g) == 2 + @test out_degree(vs[3], g) == 2 - e1 = out_edges(vs[1], g)[1] - @test source(e1, g) == vs[1] - @test target(e1, g) == vs[2] + e1 = out_edges(vs[1], g)[1] + @test source(e1, g) == vs[1] + @test target(e1, g) == vs[2] - e2 = out_edges(vs[2], g)[1] - @test source(e2, g) == vs[2] - @test target(e2, g) == vs[1] + e2 = out_edges(vs[2], g)[1] + @test source(e2, g) == vs[2] + @test target(e2, g) == vs[1] - @test edge_index(e1, g) == edge_index(e2, g) + @test edge_index(e1, g) == edge_index(e2, g) end - diff --git a/test/mst.jl b/test/mst.jl index 9321286f..1c11623b 100644 --- a/test/mst.jl +++ b/test/mst.jl @@ -19,8 +19,8 @@ wedges = [ (6, 7, 11.) ] m = length(wedges) -eweights = zeros(m) - +eweights = zeros(m) + for i = 1 : m we = wedges[i] add_edge!(g, we[1], we[2]) diff --git a/test/random.jl b/test/random.jl index f707a5ba..22f53b89 100644 --- a/test/random.jl +++ b/test/random.jl @@ -8,24 +8,24 @@ using Base.Test n = 10 p = 0.2 let g = erdos_renyi_graph(n,p, is_directed=false) - # A graph should have n vertices - @test num_vertices(g) == n - @test !is_directed(g) + # A graph should have n vertices + @test num_vertices(g) == n + @test !is_directed(g) end p = 1 let g = erdos_renyi_graph(n,p, is_directed=false) - # When p = 1, the graph should be complete. - @test num_edges(g) == sum(1:n-1) + # When p = 1, the graph should be complete. + @test num_edges(g) == sum(1:n-1) end p = 1 let g = erdos_renyi_graph(n,p, is_directed=true) - # A graph should have n vertices - @test num_vertices(g) == n - @test is_directed(g) - # When p = 1, the graph should be complete. - @test num_edges(g) == 2*sum(1:n-1) + # A graph should have n vertices + @test num_vertices(g) == n + @test is_directed(g) + # When p = 1, the graph should be complete. + @test num_edges(g) == 2*sum(1:n-1) end # Watts-Strogatz small world graphs @@ -35,19 +35,19 @@ n = 100 k = 6 beta = 0 let g = watts_strogatz_graph(n, k, beta) - @test num_edges(g) == n*(k/2) - @test num_vertices(g) == n - for i = 1:n - on = out_neighbors(i,g) - for j = 1:k/2 - @test (((i+j-1) % n) + 1 in on) - end - end + @test num_edges(g) == n*(k/2) + @test num_vertices(g) == n + for i = 1:n + on = out_neighbors(i,g) + for j = 1:k/2 + @test (((i+j-1) % n) + 1 in on) + end + end end # Check that no edges are lost in rewiring: beta = 0.1 let g = watts_strogatz_graph(n, k, beta) - @test num_edges(g) == n*(k/2) - @test num_vertices(g) == n + @test num_edges(g) == n*(k/2) + @test num_vertices(g) == n end From e767937d8fcb1f33b9509c69cf1075e7b602173c Mon Sep 17 00:00:00 2001 From: Sean Garborg Date: Mon, 27 Oct 2014 13:32:25 -0600 Subject: [PATCH 2/2] Avoid deprecation warnings on Julia 0.4 --- test/adjlist.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/adjlist.jl b/test/adjlist.jl index 16c668a7..d553212e 100644 --- a/test/adjlist.jl +++ b/test/adjlist.jl @@ -49,7 +49,7 @@ add_edge!(gd, 2, 3) @test num_edges(gd) == 3 -nbs = {[2, 3], [3], Int[]} +nbs = Any[[2, 3], [3], Int[]] for i = 1 : 3 @test out_degree(i, gd) == length(nbs[i]) @test out_neighbors(i, gd) == nbs[i] @@ -60,7 +60,7 @@ add_edge!(gu, 2, 3) @test num_edges(gu) == 2 -nbs = {[2], [1, 3], [2]} +nbs = Any[[2], [1, 3], [2]] for i = 1 : 3 @test out_degree(i, gu) == length(nbs[i]) @test out_neighbors(i, gu) == nbs[i]