You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//: In an adjacency list implementation of a graph, each vertex stores an
4
+
//: array of edges, indicating to which other vertices it has an edge.
5
+
//: (Note the directionality: we only record "to" edges, not "from" edges.)
2
6
3
-
A graph implementation, using an adjacency list.
7
+
privatevaruniqueIDCounter=0
4
8
5
-
In an adjacency list implementation, each vertex stores an array of edges, indicating to which vertices it has an edge (note the directionality). The edge stores the source and destination vertices, as well as a weight.
6
-
7
-
*/
8
-
9
-
varuniqueIDCounter:Int=0
10
-
11
-
publicstructGraphEdge<T>{
12
-
publicletfrom:GraphVertex<T>
13
-
publicletto:GraphVertex<T>
9
+
publicstructEdge<T>{
10
+
publicletfrom:Vertex<T>
11
+
publicletto:Vertex<T>
14
12
publicletweight:Double
15
13
}
16
14
17
-
18
-
publicstructGraphVertex<T>{
15
+
publicstructVertex<T>{
19
16
publicvardata:T
20
-
privatevaredges:[GraphEdge<T>]=[] // This is an adjacency list, rather than matrix
17
+
publicletuniqueID:Int
21
18
22
-
privateletuniqueID:Int
19
+
private(set)publicvaredges:[Edge<T>]=[] // the adjacency list
A graph implementation, using an adjacency matrix.
3
+
//: # Graph using an adjacency matrix
4
+
//:
5
+
//: In an adjacency matrix implementation, each vertex is given an index
6
+
//: from `0..<V`, where `V` is the total number of vertices. The matrix is
7
+
//: a 2D array, with each entry indicating if the corresponding two vertices
8
+
//: are connected, and the weight of that edge.
9
+
//:
10
+
//: For example, if `matrix[3][5] = 4.6`, then vertex #3 is connected to
11
+
//: vertex #5, with a weight of 4.6. Note that vertex #5 is not necessarily
12
+
//: connected back to vertex #3 (that would be recorded in `matrix[5][3]`).
5
13
6
-
In an adjacency matrix implementation, each vertex is associated with an index from 0..<V (V being the number of vertices). Then a matrix (a 2D array) is stored for the entire graph, with each entry indicating if the corresponding vertices are connected, and the weight. For example, if matrix[3][5] = 4.6, then vertex #3 is connected to vertex #5, with a weight of 4.6. Note that vertex #5 is not necessarily connected to vertex #3.
7
-
8
-
*/
9
-
10
-
publicstructGraphVertex<T>{
14
+
publicstructVertex<T>{
11
15
publicvardata:T
12
16
privateletindex:Int
13
17
}
14
18
15
19
publicstructGraph<T>{
16
-
17
-
// nil entries are used to mark that two vertices are NOT connected.
18
-
// If adjacencyMatrix[i][j] is not nil, then there is an edge from vertex i to vertex j.
20
+
// If adjacencyMatrix[i][j] is not nil, then there is an edge from
21
+
// vertex i to vertex j.
19
22
privatevaradjacencyMatrix:[[Double?]]=[]
20
23
21
-
// Possibly O(n^2) because of the resizing of the matrix
0 commit comments