### Install SwiftGraph via CocoaPods Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Instructions for installing the SwiftGraph library using the CocoaPods dependency manager. Ensure you have CocoaPods installed and configured for your project. ```bash pod 'SwiftGraph' ``` -------------------------------- ### Install SwiftGraph via CocoaPods Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Instructions for installing the SwiftGraph library using the CocoaPods dependency manager. Ensure you have CocoaPods installed and configured for your project. ```bash pod 'SwiftGraph' ``` -------------------------------- ### Install SwiftGraph with CocoaPods Source: https://github.com/davecom/swiftgraph/blob/master/README.md Instructions for installing SwiftGraph using CocoaPods, a dependency manager for Swift and Objective-C projects. This method is suitable for projects already using CocoaPods. ```Shell pod install SwiftGraph ``` -------------------------------- ### Install SwiftGraph via Carthage Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Instructions for integrating SwiftGraph into your project using Carthage. Add the specified line to your Cartfile and then build the framework. ```bash github "davecom/SwiftGraph" ~> 3.1 ``` -------------------------------- ### Install SwiftGraph via Carthage Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Instructions for integrating SwiftGraph into your project using Carthage. Add the specified line to your Cartfile and then build the framework. ```bash github "davecom/SwiftGraph" ~> 3.1 ``` -------------------------------- ### Implement CollectionType Protocol - startIndex Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Provides the starting index for collection-based operations, conforming to the CollectionType protocol. ```Swift public var startIndex: Int { get } Returns the index of the first element in the collection. ``` -------------------------------- ### Install SwiftGraph with Carthage Source: https://github.com/davecom/swiftgraph/blob/master/README.md Instructions for integrating SwiftGraph into your project using Carthage, a decentralized dependency manager. This involves adding the dependency to your Cartfile. ```Shell github "davecom/SwiftGraph" ~> 3.1 ``` -------------------------------- ### Manual Installation of SwiftGraph Source: https://github.com/davecom/swiftgraph/blob/master/README.md Guidance on manually adding SwiftGraph to your project by copying the source files. This method is useful for projects that do not use any dependency managers. ```Swift // Copy all of the sources in the `Sources` folder into your project. ``` -------------------------------- ### Implement CollectionType Protocol - startIndex Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Provides the starting index for collection-based operations, conforming to the CollectionType protocol. ```Swift public var startIndex: Int { get } Returns the index of the first element in the collection. ``` -------------------------------- ### PriorityQueue Initializers Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Structs/PriorityQueue.html Initializes a new PriorityQueue. The first initializer allows setting the order (ascending/descending) and starting values. The second initializer provides a custom ordering function and starting values. ```Swift public init(ascending: Bool = false, startingValues: [T] = []) ``` ```Swift public init(order: @escaping (T, T) -> Bool, startingValues: [T] = []) ``` -------------------------------- ### Breadth-First Search (BFS) Example (Swift) Source: https://github.com/davecom/swiftgraph/blob/master/README.md Performs a breadth-first search starting from a specified vertex ('New York'). The search is filtered to find vertices that meet a specific condition (city names starting with 'S'). ```swift let result = cityGraph.findAll(from: "New York") { v in return v.characters.first == "S" } ``` -------------------------------- ### PriorityQueue Initializers Source: https://github.com/davecom/swiftgraph/blob/master/docs/Structs/PriorityQueue.html Initializes a new PriorityQueue. The first initializer allows setting the order (ascending/descending) and starting values. The second initializer provides a custom ordering function and starting values. ```Swift public init(ascending: Bool = false, startingValues: [T] = []) ``` ```Swift public init(order: @escaping (T, T) -> Bool, startingValues: [T] = []) ``` -------------------------------- ### Install SwiftGraph via Swift Package Manager Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Instructions for adding SwiftGraph as a dependency using the Swift Package Manager (SPM). This is the recommended method for newer Swift projects. ```bash https://github.com/davecom/SwiftGraph ``` -------------------------------- ### Breadth-First Search (BFS) with Predicate Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Performs a breadth-first search starting from a given vertex, applying a predicate function to filter visited vertices. This example finds all cities starting with 'S' from 'New York'. ```Swift let result = cityGraph.findAll(from: "New York") { v in return v.characters.first == "S" } ``` -------------------------------- ### Install SwiftGraph via Swift Package Manager Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Instructions for adding SwiftGraph as a dependency using the Swift Package Manager (SPM). This is the recommended method for newer Swift projects. ```bash https://github.com/davecom/SwiftGraph ``` -------------------------------- ### Install SwiftGraph with Swift Package Manager Source: https://github.com/davecom/swiftgraph/blob/master/README.md Instructions for adding SwiftGraph as a dependency to your project using the Swift Package Manager (SPM). This is the recommended method for modern Swift projects. ```Swift dependencies: .package(url: "https://github.com/davecom/SwiftGraph.git", from: "3.0.0") ``` -------------------------------- ### SwiftGraph BFS All Paths Finding Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Provides methods for finding all paths from a starting vertex to multiple goal vertices using Breadth-First Search (BFS). These functions return an array of edge arrays, where each inner array represents a path to a vertex satisfying the goal test. ```APIDOC SwiftGraph BFS All Paths Finding: This section details the Breadth-First Search (BFS) functions for finding all paths to vertices that satisfy a goal test. 1. `findAllBfs(fromIndex:goalTest:)` * Finds all paths from a starting vertex index to all other vertices that satisfy a goal test using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[[E]]` - An array of arrays of Edges, where each inner array is a path to a vertex passing the goal test. Returns an empty array if no routes are found. 2. `findAllBfs(from:goalTest:)` * Finds all paths from a starting vertex (by value) to all other vertices that satisfy a goal test using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[[E]]` - An array of arrays of Edges, where each inner array is a path to a vertex passing the goal test. Returns an empty array if no routes are found. Related Methods: * `bfs`: For finding a single path. ``` ```Swift func findAllBfs(fromIndex: Int, goalTest: (V) -> Bool) -> [[E]] func findAllBfs(from: V, goalTest: (V) -> Bool) -> [[E]] ``` -------------------------------- ### Find All DFS Paths by Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Finds all path routes from a starting vertex index to other vertices that satisfy a given goal test using a depth-first search. Returns an array of arrays of Edges. ```Swift func findAllDfs(fromIndex: Int, goalTest: (V) -> Bool) -> [[E]] #### Parameters `_fromIndex_` The index of the starting vertex. `_goalTest_` Returns true if a given vertex is a goal. #### Return Value An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found ``` -------------------------------- ### SwiftGraph BFS Path Finding Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Provides multiple methods for finding a single path using Breadth-First Search (BFS). These methods allow searching from a starting vertex (specified by index or value) to a goal vertex (specified by a test function or by index/value). They return the sequence of edges forming the path. ```APIDOC SwiftGraph BFS Path Finding: This section details the Breadth-First Search (BFS) functions for finding a single path within the graph. 1. `bfs(fromIndex:goalTest:)` * Finds a route from a vertex index to the first vertex satisfying a goal test using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 2. `bfs(from:goalTest:)` * Finds a route from a vertex (by value) to the first vertex satisfying a goal test using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 3. `bfs(fromIndex:toIndex:)` * Finds a route from a starting vertex index to a specific ending vertex index using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_toIndex_`: Int - The index of the ending vertex. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 4. `bfs(from:to:)` * Finds a route from a starting vertex (by value) to a specific ending vertex (by value) using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_to_`: V - The ending vertex. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. Related Methods: * `findAllBfs`: For finding all paths to multiple goal vertices. ``` ```Swift func bfs(fromIndex: Int, goalTest: (V) -> Bool) -> [E] func bfs(from: V, goalTest: (V) -> Bool) -> [E] func bfs(fromIndex: Int, toIndex: Int) -> [E] func bfs(from: V, to: V) -> [E] ``` -------------------------------- ### SwiftGraph BFS All Paths Finding Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Provides methods for finding all paths from a starting vertex to multiple goal vertices using Breadth-First Search (BFS). These functions return an array of edge arrays, where each inner array represents a path to a vertex satisfying the goal test. ```APIDOC SwiftGraph BFS All Paths Finding: This section details the Breadth-First Search (BFS) functions for finding all paths to vertices that satisfy a goal test. 1. `findAllBfs(fromIndex:goalTest:)` * Finds all paths from a starting vertex index to all other vertices that satisfy a goal test using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[[E]]` - An array of arrays of Edges, where each inner array is a path to a vertex passing the goal test. Returns an empty array if no routes are found. 2. `findAllBfs(from:goalTest:)` * Finds all paths from a starting vertex (by value) to all other vertices that satisfy a goal test using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[[E]]` - An array of arrays of Edges, where each inner array is a path to a vertex passing the goal test. Returns an empty array if no routes are found. Related Methods: * `bfs`: For finding a single path. ``` ```Swift func findAllBfs(fromIndex: Int, goalTest: (V) -> Bool) -> [[E]] func findAllBfs(from: V, goalTest: (V) -> Bool) -> [[E]] ``` -------------------------------- ### Find All DFS Paths by Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Finds all path routes from a starting vertex to other vertices that satisfy a given goal test using a depth-first search. Returns an array of arrays of Edges. ```Swift func findAllDfs(from: V, goalTest: (V) -> Bool) -> [[E]] #### Parameters `_from_` The index of the starting vertex. `_goalTest_` Returns true if a given vertex is a goal. #### Return Value An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found ``` -------------------------------- ### SwiftGraph Find All Connected Vertices Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Uses a breadth-first search to find all connected vertices from a starting vertex that return true when evaluated by a `goalTest()` function. Returns an array of paths to these connected vertices. ```APIDOC findAll(from: Vertex, goalTest: (Vertex) -> Bool) -> [[Edge]] - Finds all vertices reachable from a starting point that meet a specific criterion. - Parameters: - from: The vertex to start the search from. - goalTest: A closure that determines if a vertex is a target. - Returns: An array of paths (each path is an array of Edges) to all vertices satisfying the `goalTest`. ``` -------------------------------- ### SwiftGraph BFS Path Finding Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Provides multiple methods for finding a single path using Breadth-First Search (BFS). These methods allow searching from a starting vertex (specified by index or value) to a goal vertex (specified by a test function or by index/value). They return the sequence of edges forming the path. ```APIDOC SwiftGraph BFS Path Finding: This section details the Breadth-First Search (BFS) functions for finding a single path within the graph. 1. `bfs(fromIndex:goalTest:)` * Finds a route from a vertex index to the first vertex satisfying a goal test using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 2. `bfs(from:goalTest:)` * Finds a route from a vertex (by value) to the first vertex satisfying a goal test using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_goalTest_`: (V) -> Bool - A closure that returns true if a given vertex is a goal. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 3. `bfs(fromIndex:toIndex:)` * Finds a route from a starting vertex index to a specific ending vertex index using BFS. * Parameters: * `_fromIndex_`: Int - The index of the starting vertex. * `_toIndex_`: Int - The index of the ending vertex. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. 4. `bfs(from:to:)` * Finds a route from a starting vertex (by value) to a specific ending vertex (by value) using BFS. * Parameters: * `_from_`: V - The starting vertex. * `_to_`: V - The ending vertex. * Returns: `[E]` - An array of Edges containing the entire route, or an empty array if no route is found. Related Methods: * `findAllBfs`: For finding all paths to multiple goal vertices. ``` ```Swift func bfs(fromIndex: Int, goalTest: (V) -> Bool) -> [E] func bfs(from: V, goalTest: (V) -> Bool) -> [E] func bfs(fromIndex: Int, toIndex: Int) -> [E] func bfs(from: V, to: V) -> [E] ``` -------------------------------- ### SwiftGraph Find All Connected Vertices Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Uses a breadth-first search to find all connected vertices from a starting vertex that return true when evaluated by a `goalTest()` function. Returns an array of paths to these connected vertices. ```APIDOC findAll(from: Vertex, goalTest: (Vertex) -> Bool) -> [[Edge]] - Finds all vertices reachable from a starting point that meet a specific criterion. - Parameters: - from: The vertex to start the search from. - goalTest: A closure that determines if a vertex is a target. - Returns: An array of paths (each path is an array of Edges) to all vertices satisfying the `goalTest`. ``` -------------------------------- ### Find All DFS Paths by Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Finds all path routes from a starting vertex index to other vertices that satisfy a given goal test using a depth-first search. Returns an array of arrays of Edges. ```Swift func findAllDfs(fromIndex: Int, goalTest: (V) -> Bool) -> [[E]] #### Parameters `_fromIndex_` The index of the starting vertex. `_goalTest_` Returns true if a given vertex is a goal. #### Return Value An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found ``` -------------------------------- ### Find All DFS Paths by Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Finds all path routes from a starting vertex to other vertices that satisfy a given goal test using a depth-first search. Returns an array of arrays of Edges. ```Swift func findAllDfs(from: V, goalTest: (V) -> Bool) -> [[E]] #### Parameters `_from_` The index of the starting vertex. `_goalTest_` Returns true if a given vertex is a goal. #### Return Value An array of arrays of Edges containing routes to every vertex connected and passing goalTest(), or an empty array if no routes could be found ``` -------------------------------- ### Breadth-First Search (BFS) with Predicate Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Performs a breadth-first search starting from a given vertex, applying a predicate function to filter visited vertices. This example finds all cities starting with 'S' from 'New York'. ```Swift let result = cityGraph.findAll(from: "New York") { v in return v.characters.first == "S" } ``` -------------------------------- ### Swift: Minimum Spanning Tree (MST) Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Classes/WeightedGraph.html Calculates the Minimum Spanning Tree (MST) of a weighted graph using Jarnik's Algorithm (Prim's Algorithm). It requires the graph to have undirected edges and assumes the graph is connected; otherwise, it spans only the connected component of the starting vertex. The function returns an array of weighted edges representing the MST or nil if the starting vertex is invalid. ```Swift func mst(start: Int = 0) -> [WeightedEdge]? --- **Description:** Find the minimum spanning tree in a weighted graph. This is the set of edges that touches every vertex in the graph and is of minimal combined weight. This function uses Jarnik’s Algorithm (aka Prim’s Algorithm) and so assumes the graph has undirected edges. For a graph with directed edges, the result may be incorrect. Also, if the graph is not fully connected, the tree will only span the connected component from which the starting vertex belongs. **Parameters:** * `start`: The index of the vertex to start creating the MST from (defaults to 0). **Return Value:** An array of WeightedEdges containing the minimum spanning tree, or nil if the starting vertex is invalid. If there is only one vertex connected to the starting vertex, an empty list is returned. ``` -------------------------------- ### SwiftGraph Implementations Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Outlines the concrete class implementations of the Graph protocol, providing ready-to-use graph data structures with varying functionalities. ```APIDOC Graph Implementations: Graph (Protocol): - The base protocol for all graph structures. - Vertices can be of any type conforming to Equatable and Codable. - Conforms to Swift's Collection and Codable protocols. - Note: Graphs are not thread-safe for modifications, but read operations are safe on constructed graphs. UnweightedGraph (Class): - A generic class implementing the Graph protocol. - Provides convenience methods for adding/removing UnweightedEdge instances. - Generic over the vertex type. WeightedGraph (Class): - A generic class implementing the Graph protocol. - Offers convenience methods for adding/removing WeightedEdge instances. - Includes a method to retrieve neighbors along with their weights. - Generic over vertex and weight types. UniqueElementsGraph (Class): - An implementation of the Graph protocol. - Supports union operations and guarantees uniqueness of vertices and edges. ``` -------------------------------- ### Initialize Weighted Graph Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Demonstrates how to create a weighted graph with String vertices and Int weights, initializing it with a list of city names. ```Swift let cityGraph: WeightedGraph = WeightedGraph(vertices: ["Seattle", "San Francisco", "Los Angeles", "Denver", "Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami", "Dallas", "Houston"]) ``` -------------------------------- ### Swift: Minimum Spanning Tree (MST) Source: https://github.com/davecom/swiftgraph/blob/master/docs/Classes/WeightedGraph.html Calculates the Minimum Spanning Tree (MST) of a weighted graph using Jarnik's Algorithm (Prim's Algorithm). It requires the graph to have undirected edges and assumes the graph is connected; otherwise, it spans only the connected component of the starting vertex. The function returns an array of weighted edges representing the MST or nil if the starting vertex is invalid. ```Swift func mst(start: Int = 0) -> [WeightedEdge]? --- **Description:** Find the minimum spanning tree in a weighted graph. This is the set of edges that touches every vertex in the graph and is of minimal combined weight. This function uses Jarnik’s Algorithm (aka Prim’s Algorithm) and so assumes the graph has undirected edges. For a graph with directed edges, the result may be incorrect. Also, if the graph is not fully connected, the tree will only span the connected component from which the starting vertex belongs. **Parameters:** * `start`: The index of the vertex to start creating the MST from (defaults to 0). **Return Value:** An array of WeightedEdges containing the minimum spanning tree, or nil if the starting vertex is invalid. If there is only one vertex connected to the starting vertex, an empty list is returned. ``` -------------------------------- ### Initialize Weighted Graph Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Demonstrates how to create a weighted graph with String vertices and Int weights, initializing it with a list of city names. ```Swift let cityGraph: WeightedGraph = WeightedGraph(vertices: ["Seattle", "San Francisco", "Los Angeles", "Denver", "Kansas City", "Chicago", "Boston", "New York", "Atlanta", "Miami", "Dallas", "Houston"]) ``` -------------------------------- ### Get Vertex by Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Retrieves a specific vertex from the graph using its integer index. ```Swift public func vertexAtIndex(_ index: Int) -> V ``` -------------------------------- ### SwiftGraph Stack Class Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Classes/Stack.html Provides documentation for the core methods and properties of the SwiftGraph Stack class, including isEmpty, push, and pop. ```APIDOC Stack Class Reference public class Stack Implements a stack - helper class that uses an array internally. Methods and Properties: - isEmpty - Description: Checks if the stack is empty. - Returns: Bool - true if the stack contains no elements, false otherwise. - Declaration: public var isEmpty: Bool { get } - push(_:) - Description: Adds an element to the top of the stack. - Parameters: - thing: The element of type T to push onto the stack. - Declaration: public func push(_ thing: T) - pop() - Description: Removes and returns the element from the top of the stack. - Returns: T - The element removed from the top of the stack. - Declaration: public func pop() -> T ``` -------------------------------- ### Get Edge Count Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Retrieves the total number of edges currently present in the graph. ```Swift public var edgeCount: Int { get } ``` -------------------------------- ### Get Vertex Count Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Retrieves the total number of vertices currently present in the graph. ```Swift public var vertexCount: Int { get } ``` -------------------------------- ### SwiftGraph Stack Class Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/Classes/Stack.html Provides documentation for the core methods and properties of the SwiftGraph Stack class, including isEmpty, push, and pop. ```APIDOC Stack Class Reference public class Stack Implements a stack - helper class that uses an array internally. Methods and Properties: - isEmpty - Description: Checks if the stack is empty. - Returns: Bool - true if the stack contains no elements, false otherwise. - Declaration: public var isEmpty: Bool { get } - push(_:) - Description: Adds an element to the top of the stack. - Parameters: - thing: The element of type T to push onto the stack. - Declaration: public func push(_ thing: T) - pop() - Description: Removes and returns the element from the top of the stack. - Returns: T - The element removed from the top of the stack. - Declaration: public func pop() -> T ``` -------------------------------- ### Get Vertex by Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Retrieves a specific vertex from the graph using its integer index. ```Swift public func vertexAtIndex(_ index: Int) -> V ``` -------------------------------- ### Get Edge Count Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Retrieves the total number of edges currently present in the graph. ```Swift public var edgeCount: Int { get } ``` -------------------------------- ### Get Vertex Count Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Retrieves the total number of vertices currently present in the graph. ```Swift public var vertexCount: Int { get } ``` -------------------------------- ### SwiftGraph Implementations Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Outlines the concrete class implementations of the Graph protocol, providing ready-to-use graph data structures with varying functionalities. ```APIDOC Graph Implementations: Graph (Protocol): - The base protocol for all graph structures. - Vertices can be of any type conforming to Equatable and Codable. - Conforms to Swift's Collection and Codable protocols. - Note: Graphs are not thread-safe for modifications, but read operations are safe on constructed graphs. UnweightedGraph (Class): - A generic class implementing the Graph protocol. - Provides convenience methods for adding/removing UnweightedEdge instances. - Generic over the vertex type. WeightedGraph (Class): - A generic class implementing the Graph protocol. - Offers convenience methods for adding/removing WeightedEdge instances. - Includes a method to retrieve neighbors along with their weights. - Generic over vertex and weight types. UniqueElementsGraph (Class): - An implementation of the Graph protocol. - Supports union operations and guarantees uniqueness of vertices and edges. ``` -------------------------------- ### Get Edge List Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Returns a collection of all edges in the graph. For undirected edges, each edge is listed only once. ```Swift public func edgeList() -> [E] ``` -------------------------------- ### Get Edge List Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Returns a collection of all edges in the graph. For undirected edges, each edge is listed only once. ```Swift public func edgeList() -> [E] ``` -------------------------------- ### SwiftPriorityQueue Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Structs/PriorityQueue.html Documentation for the core methods of the SwiftPriorityQueue class. These methods allow for efficient management of elements based on priority. ```APIDOC SwiftPriorityQueue: pop() Description: Remove and return the element with the highest priority (or lowest if ascending). O(lg n). Declaration: public mutating func pop() -> T? Return Value: The element with the highest priority in the Priority Queue, or nil if the PriorityQueue is empty. remove(_:) Description: Removes the first occurrence of a particular item. Finds it by value comparison using ==. O(n). Silently exits if no occurrence found. Declaration: public mutating func remove(_ item: T) Parameters: _item_: The item to remove the first occurrence of. removeAll(_:) Description: Removes all occurrences of a particular item. Finds it by value comparison using ==. O(n). Silently exits if no occurrence found. Declaration: public mutating func removeAll(_ item: T) Parameters: _item_: The item to remove. peek() Description: Get a look at the current highest priority item, without removing it. O(1). Declaration: public func peek() -> T? Return Value: The element with the highest priority in the PriorityQueue, or nil if the PriorityQueue is empty. clear() Description: Eliminate all of the elements from the Priority Queue. Declaration: public mutating func clear() ``` -------------------------------- ### SwiftGraph Core Protocol Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Details the essential methods and operations defined by the base Graph protocol in SwiftGraph. These methods cover vertex and edge management, lookups, and graph traversal. ```APIDOC Graph Protocol Methods: - Add Vertex: Adds a new vertex to the graph. The vertex can be of any type conforming to Equatable and Codable. - Get Vertex Index: Retrieves the integer index associated with a given vertex object. - Find Neighbors: Returns a list of vertices or their indices that are directly connected to a specified vertex or index. - Find Edges: Retrieves all edges connected to a specific vertex or index. - Check Edge Existence: Verifies if a direct edge exists between two specified vertices or indices. - Check Vertex Presence: Determines if a particular vertex is currently present in the graph. - Add Edge: Establishes a connection (edge) between two vertices, identified by their indices. - Remove All Edges Between: Deletes all edges that connect two specified vertices or indices. - Remove Vertex: Deletes a vertex from the graph. This operation is computationally expensive (O(v + e)) as it requires updating indices of all connected edges. ``` -------------------------------- ### Get Neighbors by Vertex Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Finds and returns all vertices that are direct neighbors to the vertex located at the specified index. ```Swift public func neighborsForIndex(_ index: Int) -> [V] ``` -------------------------------- ### UniqueElementsGraph init() Initializer (Swift) Source: https://github.com/davecom/swiftgraph/blob/master/docs/Classes/UniqueElementsGraph.html Initializes a new instance of the UniqueElementsGraph. This initializer is currently undocumented. ```Swift public init() ``` -------------------------------- ### Get Neighbors by Vertex Index Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Finds and returns all vertices that are direct neighbors to the vertex located at the specified index. ```Swift public func neighborsForIndex(_ index: Int) -> [V] ``` -------------------------------- ### SwiftPriorityQueue Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/Structs/PriorityQueue.html Documentation for the core methods of the SwiftPriorityQueue class. These methods allow for efficient management of elements based on priority. ```APIDOC SwiftPriorityQueue: pop() Description: Remove and return the element with the highest priority (or lowest if ascending). O(lg n). Declaration: public mutating func pop() -> T? Return Value: The element with the highest priority in the Priority Queue, or nil if the PriorityQueue is empty. remove(_:) Description: Removes the first occurrence of a particular item. Finds it by value comparison using ==. O(n). Silently exits if no occurrence found. Declaration: public mutating func remove(_ item: T) Parameters: _item_: The item to remove the first occurrence of. removeAll(_:) Description: Removes all occurrences of a particular item. Finds it by value comparison using ==. O(n). Silently exits if no occurrence found. Declaration: public mutating func removeAll(_ item: T) Parameters: _item_: The item to remove. peek() Description: Get a look at the current highest priority item, without removing it. O(1). Declaration: public func peek() -> T? Return Value: The element with the highest priority in the PriorityQueue, or nil if the PriorityQueue is empty. clear() Description: Eliminate all of the elements from the Priority Queue. Declaration: public mutating func clear() ``` -------------------------------- ### Graph Search Algorithms (BFS, DFS, Dijkstra) Source: https://github.com/davecom/swiftgraph/blob/master/README.md Documents the search algorithms available for SwiftGraph, including Breadth-First Search (BFS), Depth-First Search (DFS), and Dijkstra's algorithm for weighted graphs. These methods are typically found in extensions and provide pathfinding capabilities. ```APIDOC Graph Search Methods: bfs(from:to:goalTest:) - Finds a path from a source vertex to a destination vertex using Breadth-First Search. - Can optionally use a `goalTest` function to find the first vertex satisfying a condition. - Parameters: - from: The starting vertex. - to: The destination vertex (optional, if `goalTest` is used). - goalTest: A closure that returns true for a goal vertex (optional). - Returns: An array of `Edge` objects representing the path, or an empty array if no path is found. dfs(from:to:goalTest:) - Finds a path from a source vertex to a destination vertex using Depth-First Search. - Can optionally use a `goalTest` function to find the first vertex satisfying a condition. - Parameters: - from: The starting vertex. - to: The destination vertex (optional, if `goalTest` is used). - goalTest: A closure that returns true for a goal vertex (optional). - Returns: An array of `Edge` objects representing the path, or an empty array if no path is found. findAll(from:goalTest:) - Uses BFS to find all connected vertices reachable from a starting vertex that satisfy a `goalTest` function. - Parameters: - from: The starting vertex. - goalTest: A closure that returns true for a goal vertex. - Returns: An array of paths (each path is an array of `Edge` objects) to connected goal vertices. dijkstra(from:) - Finds the shortest path from a starting vertex to all other vertices in a `WeightedGraph`. - Parameters: - from: The starting vertex index. - Returns: A tuple containing: - distances: An array of shortest distances to each vertex, indexed by vertex index. - pathDictionary: A dictionary mapping vertex indices to the preceding `Edge` in the shortest path. - Related Methods: - `pathDictToPath(from:to:pathDict:)` can be used with the `pathDictionary` to reconstruct a specific shortest path. ``` -------------------------------- ### SwiftGraph Core Protocol Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/index.html Details the essential methods and operations defined by the base Graph protocol in SwiftGraph. These methods cover vertex and edge management, lookups, and graph traversal. ```APIDOC Graph Protocol Methods: - Add Vertex: Adds a new vertex to the graph. The vertex can be of any type conforming to Equatable and Codable. - Get Vertex Index: Retrieves the integer index associated with a given vertex object. - Find Neighbors: Returns a list of vertices or their indices that are directly connected to a specified vertex or index. - Find Edges: Retrieves all edges connected to a specific vertex or index. - Check Edge Existence: Verifies if a direct edge exists between two specified vertices or indices. - Check Vertex Presence: Determines if a particular vertex is currently present in the graph. - Add Edge: Establishes a connection (edge) between two vertices, identified by their indices. - Remove All Edges Between: Deletes all edges that connect two specified vertices or indices. - Remove Vertex: Deletes a vertex from the graph. This operation is computationally expensive (O(v + e)) as it requires updating indices of all connected edges. ``` -------------------------------- ### SwiftGraph CollectionType Protocols Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Structs/PriorityQueue.html Details the implementation of Swift's CollectionType protocols within SwiftGraph. This includes the definition of Index, startIndex, endIndex, subscript access, and the index(after:) method, essential for sequence and collection operations. ```Swift public typealias Index = Int public var startIndex: Int { get } public var endIndex: Int { get } public subscript(i: Int) -> T { get } public func index(after i: PriorityQueue.Index) -> PriorityQueue.Index ``` -------------------------------- ### Get Neighbors by Vertex Value Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Finds and returns all vertices that are direct neighbors to a given vertex value. Returns nil if the vertex is not found. ```Swift public func neighborsForVertex(_ vertex: V) -> [V]? ``` -------------------------------- ### Get Index of Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Finds the first occurrence of a given vertex within the graph and returns its index. Returns nil if the vertex is not found. ```Swift public func indexOfVertex(_ vertex: V) -> Int? ``` -------------------------------- ### UniqueElementsGraph init() Initializer (Swift) Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Classes/UniqueElementsGraph.html Initializes a new instance of the UniqueElementsGraph. This initializer is currently undocumented. ```Swift public init() ``` -------------------------------- ### Get Index of Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Finds the first occurrence of a given vertex within the graph and returns its index. Returns nil if the vertex is not found. ```Swift public func indexOfVertex(_ vertex: V) -> Int? ``` -------------------------------- ### Get Neighbors by Vertex Value Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Finds and returns all vertices that are direct neighbors to a given vertex value. Returns nil if the vertex is not found. ```Swift public func neighborsForVertex(_ vertex: V) -> [V]? ``` -------------------------------- ### SwiftGraph DFS Pathfinding Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Finds a path from a source vertex to a destination vertex in a Graph using Depth-First Search (DFS). Returns an array of Edges representing the path, or an empty array if no path exists. An overload accepts a `goalTest()` function to find a path to the first vertex satisfying the condition. ```APIDOC dfs(from: Vertex, to: Vertex) -> [Edge] - Finds a path using Depth-First Search. - Parameters: - from: The starting vertex. - to: The destination vertex. - Returns: An array of Edges forming the path, or an empty array if no path is found. dfs(from: Vertex, goalTest: (Vertex) -> Bool) -> [Edge] - Finds a path to the first vertex that satisfies the `goalTest` condition. - Parameters: - from: The starting vertex. - goalTest: A closure that returns true for the target vertex. - Returns: An array of Edges forming the path to the goal vertex, or an empty array if no goal is found. ``` -------------------------------- ### SwiftGraph: Get Edges for Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Retrieves all edges associated with a given vertex. It accepts a vertex of type V and returns an optional array of edges. ```Swift public func edgesForVertex(_ vertex: V) -> [E]? ``` -------------------------------- ### SwiftGraph: Get Edges for Vertex Source: https://github.com/davecom/swiftgraph/blob/master/docs/Protocols/Graph.html Retrieves all edges associated with a given vertex. It accepts a vertex of type V and returns an optional array of edges. ```Swift public func edgesForVertex(_ vertex: V) -> [E]? ``` -------------------------------- ### SwiftGraph BFS Pathfinding Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Finds a path from a source vertex to a destination vertex in a Graph using Breadth-First Search (BFS). Returns an array of Edges representing the path, or an empty array if no path exists. An overload accepts a `goalTest()` function to find a path to the first vertex satisfying the condition. ```APIDOC bfs(from: Vertex, to: Vertex) -> [Edge] - Finds a path using Breadth-First Search. - Parameters: - from: The starting vertex. - to: The destination vertex. - Returns: An array of Edges forming the path, or an empty array if no path is found. bfs(from: Vertex, goalTest: (Vertex) -> Bool) -> [Edge] - Finds a path to the first vertex that satisfies the `goalTest` condition. - Parameters: - from: The starting vertex. - goalTest: A closure that returns true for the target vertex. - Returns: An array of Edges forming the path to the goal vertex, or an empty array if no goal is found. ``` -------------------------------- ### Implement CustomStringConvertible Protocol Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/Protocols/Graph.html Provides a computed property 'description' to return a string representation of the object, conforming to the CustomStringConvertible protocol. ```Swift public var description: String { get } This property is part of the CustomStringConvertible protocol, offering a textual representation of the graph or its components. ``` -------------------------------- ### Check if Graph is DAG (Swift) Source: https://github.com/davecom/swiftgraph/blob/master/README.md Determines whether the graph is a Directed Acyclic Graph (DAG). The example shows that the `cityGraph` is not a DAG, as indicated by the `isADAG` property returning false. ```swift let isADAG = cityGraph.isDAG ``` -------------------------------- ### WeightedGraph Class Methods Source: https://github.com/davecom/swiftgraph/blob/master/docs/docsets/SwiftGraph.docset/Contents/Resources/Documents/index.html Details on methods available for the WeightedGraph class, including adding edges, finding vertex indices, and performing graph analysis. ```APIDOC WeightedGraph: Represents a graph where edges have associated weights. - addEdge(from:to:weight:) - Adds a directed edge between two vertices with a specified weight. - Parameters: - from: The source vertex. - to: The destination vertex. - weight: The weight associated with the edge. - indexOfVertex(vertex:) - Returns the index of a given vertex in the graph's internal vertex list. - Parameters: - vertex: The vertex to find the index for. - Returns: The index of the vertex, or nil if not found. - edgesToVertices(edges:) - Converts an array of edges into an array of vertex labels. - Parameters: - edges: An array of WeightedEdge objects. - Returns: An array of vertex labels. - mst() - Computes the Minimum Spanning Tree (MST) of the graph. - Returns: An array of WeightedEdge objects representing the MST. - detectCycles() - Detects and returns all cycles in the graph. - Returns: An array of arrays, where each inner array represents a cycle. - isDAG - A computed property that returns true if the graph is a Directed Acyclic Graph (DAG), false otherwise. - Returns: Bool. - findAll(from:predicate:) - Performs a search (typically BFS) starting from a vertex and returns all vertices that satisfy a given predicate. - Parameters: - from: The starting vertex for the search. - predicate: A closure that takes a vertex and returns true if it matches the criteria. - Returns: An array of vertices that satisfy the predicate. ```