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

add scalar and tensor products #239

Merged
merged 1 commit into from
Nov 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ write(g,"mygraph.jgz")

- **connectivity:** strongly- and weakly-connected components, bipartite checks, condensation, attracting components

- **operators:** complement, reverse, reverse!, union, join, intersect, difference, symmetric difference, blkdiag, induced subgraphs
- **operators:** complement, reverse, reverse!, union, join, intersect, difference,
symmetric difference, blkdiag, induced subgraphs, products (cartesian/scalar)

- **shortest paths:** Dijkstra, Dijkstra with predecessors, Bellman-Ford, Floyd-Warshall, A*

Expand Down
2 changes: 1 addition & 1 deletion src/LightGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ eccentricity, diameter, periphery, radius, center,
# operators
complement, reverse, reverse!, union, intersect,
difference, symmetric_difference,
induced_subgraph, join,
induced_subgraph, join, tensor_product, cartesian_product,

# graph visit
SimpleGraphVisitor, TrivialGraphVisitor, LogGraphVisitor,
Expand Down
40 changes: 40 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,43 @@ eltype(g::SimpleGraph) = Float64
length(g::SimpleGraph) = nv(g)*nv(g)
ndims(g::SimpleGraph) = 2
issym(g::SimpleGraph) = !is_directed(g)

"""
Returns the (cartesian product)[https://en.wikipedia.org/wiki/Tensor_product_of_graphs] of `g` and `h`
"""
function cartesian_product{G<:SimpleGraph}(g::G, h::G)
z = G(nv(g)*nv(h))
id(i, j) = (i-1)*nv(h) + j
for e in edges(g)
i1, i2 = src(e), dst(e)
for j=1:nv(h)
add_edge!(z, id(i1,j), id(i2,j))
end
end

for e in edges(h)
j1, j2 = src(e), dst(e)
for i=1:nv(g)
add_edge!(z, id(i,j1), id(i,j2))
end
end

z
end

"""
Returns the (tensor product)[https://en.wikipedia.org/wiki/Tensor_product_of_graphs] of `g` and `h`
"""
function tensor_product{G<:SimpleGraph}(g::G, h::G)
z = G(nv(g)*nv(h))
id(i, j) = (i-1)*nv(h) + j
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does putting a function inside another function do something to efficiency/dispatch? That is, see JuliaLang/julia#4428

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did the benchmak against the same code without closures, and thhe version with closures seems to be slightly faster (don't ask me why).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, works for me :)

for eg in edges(g)
i1, i2 = src(eg), dst(eg)
for eh in edges(h)
j1, j2 = src(eh), dst(eh)
add_edge!(z, id(i1,j1), id(i2,j2))
end
end

z
end
10 changes: 10 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ x = p*ones(10)
@test ndims(p) == 2
@test issym(p)
@test !issym(g5)

g22 = CompleteGraph(2)
h = cartesian_product(g22, g22)
@test nv(h) == 4
@test ne(h)== 4

g22 = CompleteGraph(2)
h = tensor_product(g22, g22)
@test nv(h) == 4
@test ne(h) == 1