Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
from scipy import sparse | ||
|
||
from . import Graph # prevent circular import in Python < 3.5 | ||
|
||
|
||
class LineGraph(Graph): | ||
r"""Build the line graph of a graph. | ||
Each vertex of the line graph represents an edge in the original graph. Two | ||
vertices are connected if the edges they represent share a vertex in the | ||
original graph. | ||
Parameters | ||
---------- | ||
graph : :class:`Graph` | ||
Examples | ||
-------- | ||
>>> import matplotlib.pyplot as plt | ||
>>> graph = graphs.Sensor(5, k=2, seed=10) | ||
>>> line_graph = graphs.LineGraph(graph) | ||
>>> fig, ax = plt.subplots() | ||
>>> fig, ax = graph.plot('blue', edge_color='blue', indices=True, ax=ax) | ||
>>> fig, ax = line_graph.plot('red', edge_color='red', indices=True, ax=ax) | ||
>>> _ = ax.set_title('graph and its line graph') | ||
""" | ||
|
||
def __init__(self, graph, **kwargs): | ||
|
||
graph.compute_differential_operator() | ||
# incidence = np.abs(graph.D) # weighted? | ||
incidence = (graph.D != 0) | ||
|
||
adjacency = incidence.T.dot(incidence).astype(np.int) | ||
adjacency -= sparse.identity(graph.n_edges, dtype=np.int) | ||
|
||
try: | ||
coords = incidence.T.dot(graph.coords) / 2 | ||
except AttributeError: | ||
coords = None | ||
|
||
super(LineGraph, self).__init__(adjacency, coords=coords, | ||
plotting=graph.plotting, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters