Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Graph.swift #1007

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Topological Sort/Graph.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@

/**
* A class that represents a graph data structure.
* It contains an adjacency list which is a dictionary of nodes and their corresponding edges.
* It has the following methods:
* - addNode(value: Node) -> Node: adds a new node to the graph
* - addEdge(fromNode: Node, toNode: Node) -> Bool: adds a directed edge from one node to another
* - adjacencyList(forNode: Node) -> [Node]? : returns the list of nodes that are adjacent to the given node
* - calculateInDegreeOfNodes() -> [Node : InDegree]: returns a dictionary of nodes and their corresponding in-degree values
*
*/

public class Graph: CustomStringConvertible {
public typealias Node = String

Expand Down Expand Up @@ -33,6 +45,13 @@ public class Graph: CustomStringConvertible {

extension Graph {
typealias InDegree = Int

/**
* A method that calculates the in-degree of all the nodes in the graph.
* It returns a dictionary with the nodes as keys and their corresponding in-degree values as values.
*
* - returns: A dictionary of [Node : InDegree]
*/

func calculateInDegreeOfNodes() -> [Node : InDegree] {
var inDegrees = [Node: InDegree]()
Expand Down