Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ benchmark/Manifest.toml
/docs/src/index.md
/docs/src/contributing.md
/docs/src/license.md
.aider*
Comment thread
simonschoelly marked this conversation as resolved.
Outdated
244 changes: 244 additions & 0 deletions Manifest.toml
Comment thread
lampretl marked this conversation as resolved.
Outdated

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Inflate = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Comment thread
lampretl marked this conversation as resolved.
Outdated
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
Expand Down
1 change: 1 addition & 0 deletions src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export
egonet,
merge_vertices!,
merge_vertices,
line_graph,

# bfs
gdistances,
Expand Down
85 changes: 85 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,88 @@ function merge_vertices!(g::Graph{T}, vs::Vector{U} where {U<:Integer}) where {T

return new_vertex_ids
end

"""
line_graph(g::SimpleGraph) ::SimpleGraph
Given a graph `g`, return the graph `lg`, whose vertices are integers that enumerate the
edges in `g`, and two vertices in `lg` form an edge iff the corresponding edges in `g`
share a common endpoint. In other words, edges in `lg` are length-2 paths in `g`.
Note that `i ∈ vertices(lg)` corresponds to `collect(edges(g))[i]`.

# Examples
```jldoctest
julia> using Graphs

julia> g = path_graph(5);

julia> lg = line_graph(g)
{4, 3} undirected simple Int64 graph
```
"""
function line_graph(g::SimpleGraph)
vertex_to_edges = [Int[] for _ in 1:nv(g)]
for (i, e) in enumerate(edges(g))
s, d = src(e), dst(e)
push!(vertex_to_edges[s], i)
s == d && continue # do not push self-loops twice
push!(vertex_to_edges[d], i)
end

fadjlist = [Int[] for _ in 1:ne(g)] # edge to neighbors adjacency in lg
m = 0 # number of edges in the line-graph
for es in vertex_to_edges
n = length(es)
for i in 1:(n - 1), j in (i + 1):n # iterate through pairs of edges with same endpoint
ei, ej = es[i], es[j]
m += 1
push!(fadjlist[ei], ej)
push!(fadjlist[ej], ei)
end
end

foreach(sort!, fadjlist)
return SimpleGraph(m, fadjlist)
end

"""
line_graph(g::SimpleDiGraph) ::SimpleDiGraph
Given a digraph `g`, return the digraph `lg`, whose vertices are integers that enumerate
the edges in `g`, and there is an edge in `lg` from `Edge(a,b)` to `Edge(c,d)` iff b==c.
In other words, edges in `lg` are length-2 directed paths in `g`.
Note that `i ∈ vertices(lg)` corresponds to `collect(edges(g))[i]`.

# Examples
```jldoctest
julia> using Graphs

julia> g = cycle_digraph(5);

julia> lg = line_graph(g)
{5, 5} directed simple Int64 graph
```
"""
function line_graph(g::SimpleDiGraph)
vertex_to_edgesout = [Int[] for _ in 1:nv(g)]
vertex_to_edgesin = [Int[] for _ in 1:nv(g)]
for (i, e) in enumerate(edges(g))
s, d = src(e), dst(e)
push!(vertex_to_edgesout[s], i)
push!(vertex_to_edgesin[d], i)
end

fadjilist = [Int[] for _ in 1:ne(g)] # edge to neighbors forward adjacency in lg
badjilist = [Int[] for _ in 1:ne(g)] # edge to neighbors backward adjacency in lg
m = 0 # number of edges in the line-graph
for (e_i, e_o) in zip(vertex_to_edgesin, vertex_to_edgesout)
for ei in e_i, eo in e_o # iterate through length-2 directed paths
ei == eo && continue # a self-loop in g does not induce a self-loop in lg
m += 1
push!(fadjilist[ei], eo)
push!(badjilist[eo], ei)
end
end

foreach(sort!, fadjilist)
foreach(sort!, badjilist)
return SimpleDiGraph(m, fadjilist, badjilist)
end
Loading
Loading