Skip to content

Commit 8c0a065

Browse files
committed
Disconnect nodes
1 parent dcec19c commit 8c0a065

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Dijkstra.NET/Graph/Graph.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ public bool Connect(uint from, uint to, int cost, TEdgeCustom custom)
6969
return true;
7070
}
7171

72+
/// <summary>
73+
/// Disconnect node to from node from
74+
/// </summary>
75+
/// <param name="from">First node</param>
76+
/// <param name="to">Second node</param>
77+
/// <returns>Returns true if nodes disconnected</returns>
78+
public bool Disconnect(uint from, uint to)
79+
{
80+
if (!_nodes.ContainsKey(from) || !_nodes.ContainsKey(to))
81+
return false;
82+
83+
Node<T,TEdgeCustom> nodeFrom = _nodes[from];
84+
Node<T, TEdgeCustom> nodeTo = _nodes[to];
85+
86+
return nodeTo.RemoveParent(nodeFrom) & nodeFrom.RemoveEdge(to);
87+
}
88+
7289
public IEnumerator<INode<T, TEdgeCustom>> GetEnumerator() => _nodes.Select(x => x.Value).GetEnumerator();
7390

7491
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

src/Dijkstra.NET/Graph/Node.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,30 @@ internal void AddEdge(in Edge<T, TEdgeCustom> edge)
9494
EdgesCount++;
9595
}
9696

97+
internal bool RemoveEdge(in uint to)
98+
{
99+
for (int i = 0; i < EdgesCount; i++)
100+
{
101+
if (_edges[i].Node.Key == to)
102+
{
103+
EdgesCount--;
104+
_edges[i] = _edges[EdgesCount];
105+
return true;
106+
}
107+
}
108+
return true;
109+
}
110+
97111
internal void AddParent(Node<T, TEdgeCustom> parent)
98112
{
99113
_parents.Add(parent);
100114
}
101115

116+
internal bool RemoveParent(Node<T, TEdgeCustom> parent)
117+
{
118+
return _parents.Remove(parent);
119+
}
120+
102121
public override int GetHashCode()
103122
{
104123
return Key.GetHashCode();

0 commit comments

Comments
 (0)