Skip to content

Commit 7ee1dd9

Browse files
authored
Merge pull request #85 from TizianoCoroneo/tiziano/indegree-outdegree
feat: indegree and outdegree functions
2 parents 59414e2 + 049884f commit 7ee1dd9

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Sources/SwiftGraph/Graph.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ extension Graph {
236236
removeVertexAtIndex(i)
237237
}
238238
}
239+
240+
/// How many edges end at the vertex at this index? Undirected edges are considered as ending at both vertices.
241+
///
242+
/// - Parameter index: Index of the vertex.
243+
/// - Returns: The count of edges that end at that vertex.
244+
public func indegreeOfVertex(at index: Int) -> Int {
245+
var count = 0
246+
for edgesForVertex in edges {
247+
for edge in edgesForVertex where edge.v == index {
248+
count += 1
249+
}
250+
}
251+
return count
252+
}
253+
254+
/// How many edges start at the vertex at this index? Undirected edges are considered as starting at both vertices.
255+
///
256+
/// - Parameter index: Index of the vertex.
257+
/// - Returns: The count of edges that start at that vertex.
258+
public func outdegreeOfVertex(at index: Int) -> Int {
259+
edges[index].count
260+
}
239261

240262
/// Check whether an edge is in the graph or not.
241263
///

Tests/SwiftGraphTests/SwiftGraphTests.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,44 @@ class SwiftGraphTests: XCTestCase {
7171
XCTAssertEqual(g.vertexCount, 2, "2 total vertices")
7272
XCTAssertEqual(g.edgeCount, 1, "1 total edges")
7373
}
74-
74+
75+
func testIndegreeAndOutdegree() {
76+
var g = UnweightedGraph<String>()
77+
let atlantaIndex = g.addVertex("Atlanta")
78+
let nyIndex = g.addVertex("New York")
79+
let miamiIndex = g.addVertex("Miami")
80+
let sfIndex = g.addVertex("San Francisco")
81+
let arezzoIndex = g.addVertex("Arezzo")
82+
g.addEdge(from: "Atlanta", to: "New York", directed: true)
83+
let nyAtlantaEdge = UnweightedEdge(u: nyIndex, v: atlantaIndex, directed: true)
84+
g.addEdge(nyAtlantaEdge, directed: true)
85+
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
86+
g.addEdge(from: "New York", to: "Miami", directed: false)
87+
g.addEdge(from: "Atlanta", to: "Miami", directed: true)
88+
g.addEdge(from: "San Francisco", to: "Miami", directed: false)
89+
XCTAssertEqual(g.indegreeOfVertex(at: atlantaIndex), 2, "2 edges end at Atlanta")
90+
XCTAssertEqual(g.indegreeOfVertex(at: miamiIndex), 3, "3 edges end at Miami")
91+
XCTAssertEqual(g.indegreeOfVertex(at: sfIndex), 1, "1 edge ends at San Francisco")
92+
XCTAssertEqual(g.indegreeOfVertex(at: nyIndex), 2, "2 edges end at New York")
93+
XCTAssertEqual(g.indegreeOfVertex(at: arezzoIndex), 0, "0 edges end at Arezzo")
94+
XCTAssertEqual(g.outdegreeOfVertex(at: atlantaIndex), 2, "2 edges start from Atlanta")
95+
XCTAssertEqual(g.outdegreeOfVertex(at: miamiIndex), 3, "3 edges start from Miami")
96+
XCTAssertEqual(g.outdegreeOfVertex(at: sfIndex), 1, "1 edge starts from San Francisco")
97+
XCTAssertEqual(g.outdegreeOfVertex(at: nyIndex), 2, "2 edges start from New York")
98+
XCTAssertEqual(g.outdegreeOfVertex(at: arezzoIndex), 0, "0 edges start from Arezzo")
99+
g.removeEdge(nyAtlantaEdge)
100+
XCTAssertEqual(g.indegreeOfVertex(at: atlantaIndex), 1, "1 edgee ends at Atlanta")
101+
XCTAssertEqual(g.indegreeOfVertex(at: miamiIndex), 3, "3 edges end at Miami")
102+
XCTAssertEqual(g.indegreeOfVertex(at: sfIndex), 1, "1 edge ends at San Francisco")
103+
XCTAssertEqual(g.indegreeOfVertex(at: nyIndex), 2, "2 edges end at New York")
104+
XCTAssertEqual(g.indegreeOfVertex(at: arezzoIndex), 0, "0 edges end at Arezzo")
105+
XCTAssertEqual(g.outdegreeOfVertex(at: atlantaIndex), 2, "2 edges start from Atlanta")
106+
XCTAssertEqual(g.outdegreeOfVertex(at: miamiIndex), 3, "3 edges start from Miami")
107+
XCTAssertEqual(g.outdegreeOfVertex(at: sfIndex), 1, "1 edge starts from San Francisco")
108+
XCTAssertEqual(g.outdegreeOfVertex(at: nyIndex), 1, "1 edge starts from New York")
109+
XCTAssertEqual(g.outdegreeOfVertex(at: arezzoIndex), 0, "0 edges start from Arezzo")
110+
}
111+
75112
func testSubscript() {
76113
let g: UnweightedGraph<String> = UnweightedGraph<String>()
77114
_ = g.addVertex("Atlanta")

0 commit comments

Comments
 (0)