Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #124 from garborg/master
Browse files Browse the repository at this point in the history
Update syntax for Julia 0.4, clean up whitespace
  • Loading branch information
lindahua committed Oct 28, 2014
2 parents 4de1cbc + e767937 commit 0acf4cd
Show file tree
Hide file tree
Showing 34 changed files with 194 additions and 222 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.

3 changes: 1 addition & 2 deletions src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -130,4 +130,3 @@ module Graphs

include("random.jl")
end

5 changes: 2 additions & 3 deletions src/adjacency_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

8 changes: 4 additions & 4 deletions src/bellmanford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/breadth_first_visit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,3 @@ function gdistances(graph::AbstractGraph, sources; defaultdist::Int=-1)
dists = fill(defaultdist, num_vertices(graph))
gdistances!(graph, sources, dists)
end

11 changes: 5 additions & 6 deletions src/concepts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -76,4 +76,3 @@ end
macro graph_requires(g, concepts...)
esc(_graph_requires_code(g, concepts...))
end

2 changes: 1 addition & 1 deletion src/connected_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/depth_first_visit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,3 @@ function topological_sort_by_dfs{V}(graph::AbstractGraph{V})

reverse(visitor.vertices)
end

1 change: 0 additions & 1 deletion src/dijkstra_spath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 2 additions & 4 deletions src/edge_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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))


49 changes: 24 additions & 25 deletions src/floyd_warshall.jl
Original file line number Diff line number Diff line change
@@ -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))

Loading

0 comments on commit 0acf4cd

Please sign in to comment.