Skip to content

Commit 7d00d6d

Browse files
committed
Move parallel edges algorithm helpers to Algorithm\Parallel
1 parent af1d244 commit 7d00d6d

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/Parallel.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,57 @@ class Parallel extends BaseGraph
2626
public function hasEdgeParallel()
2727
{
2828
foreach ($this->graph->getEdges() as $edge) {
29-
if ($edge->hasEdgeParallel()) {
29+
if ($this->hasEdgeParallelEdge($edge)) {
3030
return true;
3131
}
3232
}
3333

3434
return false;
3535
}
36+
37+
38+
/**
39+
* checks whether this edge has any parallel edges
40+
*
41+
* @return boolean
42+
* @uses Edge::getEdgesParallel()
43+
*/
44+
public function hasEdgeParallelEdge(Edge $edge)
45+
{
46+
return !!$this->getEdgesParallelEdge($edge);
47+
}
48+
49+
/**
50+
* get all edges parallel to this edge (excluding self)
51+
*
52+
* @return Edge[]
53+
* @throws LogicException
54+
*/
55+
public function getEdgesParallelEdge(Edge $edge)
56+
{
57+
$ends = $edge->getVertices();
58+
59+
// get all edges between this edge's endpoints
60+
$edges = $ends[0]->getEdgesTo($ends[1]);
61+
// edge points into both directions (undirected/bidirectional edge)
62+
if ($edge->isConnection($ends[1], $ends[0])) {
63+
// also get all edges in other direction
64+
$back = $ends[1]->getEdgesTo($ends[0]);
65+
foreach ($back as $edgee) {
66+
if (!in_array($edgee, $edges)) {
67+
$edges[] = $edgee;
68+
}
69+
} // alternative implementation for array_unique(), because it requires casting edges to string
70+
}
71+
72+
$pos = array_search($edge, $edges, true);
73+
if ($pos === false) {
74+
throw new LogicException('Internal error: Current edge not found');
75+
}
76+
77+
// exclude current edge from parallel edges
78+
unset($edges[$pos]);
79+
80+
return array_values($edges);
81+
}
3682
}

0 commit comments

Comments
 (0)