Skip to content

Commit

Permalink
Merge pull request #85 from TizianoCoroneo/tiziano/indegree-outdegree
Browse files Browse the repository at this point in the history
feat: indegree and outdegree functions
  • Loading branch information
davecom authored May 5, 2024
2 parents 59414e2 + 049884f commit 7ee1dd9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Sources/SwiftGraph/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ extension Graph {
removeVertexAtIndex(i)
}
}

/// How many edges end at the vertex at this index? Undirected edges are considered as ending at both vertices.
///
/// - Parameter index: Index of the vertex.
/// - Returns: The count of edges that end at that vertex.
public func indegreeOfVertex(at index: Int) -> Int {
var count = 0
for edgesForVertex in edges {
for edge in edgesForVertex where edge.v == index {
count += 1
}
}
return count
}

/// How many edges start at the vertex at this index? Undirected edges are considered as starting at both vertices.
///
/// - Parameter index: Index of the vertex.
/// - Returns: The count of edges that start at that vertex.
public func outdegreeOfVertex(at index: Int) -> Int {
edges[index].count
}

/// Check whether an edge is in the graph or not.
///
Expand Down
39 changes: 38 additions & 1 deletion Tests/SwiftGraphTests/SwiftGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,44 @@ class SwiftGraphTests: XCTestCase {
XCTAssertEqual(g.vertexCount, 2, "2 total vertices")
XCTAssertEqual(g.edgeCount, 1, "1 total edges")
}


func testIndegreeAndOutdegree() {
var g = UnweightedGraph<String>()
let atlantaIndex = g.addVertex("Atlanta")
let nyIndex = g.addVertex("New York")
let miamiIndex = g.addVertex("Miami")
let sfIndex = g.addVertex("San Francisco")
let arezzoIndex = g.addVertex("Arezzo")
g.addEdge(from: "Atlanta", to: "New York", directed: true)
let nyAtlantaEdge = UnweightedEdge(u: nyIndex, v: atlantaIndex, directed: true)
g.addEdge(nyAtlantaEdge, directed: true)
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
g.addEdge(from: "New York", to: "Miami", directed: false)
g.addEdge(from: "Atlanta", to: "Miami", directed: true)
g.addEdge(from: "San Francisco", to: "Miami", directed: false)
XCTAssertEqual(g.indegreeOfVertex(at: atlantaIndex), 2, "2 edges end at Atlanta")
XCTAssertEqual(g.indegreeOfVertex(at: miamiIndex), 3, "3 edges end at Miami")
XCTAssertEqual(g.indegreeOfVertex(at: sfIndex), 1, "1 edge ends at San Francisco")
XCTAssertEqual(g.indegreeOfVertex(at: nyIndex), 2, "2 edges end at New York")
XCTAssertEqual(g.indegreeOfVertex(at: arezzoIndex), 0, "0 edges end at Arezzo")
XCTAssertEqual(g.outdegreeOfVertex(at: atlantaIndex), 2, "2 edges start from Atlanta")
XCTAssertEqual(g.outdegreeOfVertex(at: miamiIndex), 3, "3 edges start from Miami")
XCTAssertEqual(g.outdegreeOfVertex(at: sfIndex), 1, "1 edge starts from San Francisco")
XCTAssertEqual(g.outdegreeOfVertex(at: nyIndex), 2, "2 edges start from New York")
XCTAssertEqual(g.outdegreeOfVertex(at: arezzoIndex), 0, "0 edges start from Arezzo")
g.removeEdge(nyAtlantaEdge)
XCTAssertEqual(g.indegreeOfVertex(at: atlantaIndex), 1, "1 edgee ends at Atlanta")
XCTAssertEqual(g.indegreeOfVertex(at: miamiIndex), 3, "3 edges end at Miami")
XCTAssertEqual(g.indegreeOfVertex(at: sfIndex), 1, "1 edge ends at San Francisco")
XCTAssertEqual(g.indegreeOfVertex(at: nyIndex), 2, "2 edges end at New York")
XCTAssertEqual(g.indegreeOfVertex(at: arezzoIndex), 0, "0 edges end at Arezzo")
XCTAssertEqual(g.outdegreeOfVertex(at: atlantaIndex), 2, "2 edges start from Atlanta")
XCTAssertEqual(g.outdegreeOfVertex(at: miamiIndex), 3, "3 edges start from Miami")
XCTAssertEqual(g.outdegreeOfVertex(at: sfIndex), 1, "1 edge starts from San Francisco")
XCTAssertEqual(g.outdegreeOfVertex(at: nyIndex), 1, "1 edge starts from New York")
XCTAssertEqual(g.outdegreeOfVertex(at: arezzoIndex), 0, "0 edges start from Arezzo")
}

func testSubscript() {
let g: UnweightedGraph<String> = UnweightedGraph<String>()
_ = g.addVertex("Atlanta")
Expand Down

0 comments on commit 7ee1dd9

Please sign in to comment.