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
Copy file name to clipboardExpand all lines: Graph/README.markdown
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ With this hypothetical airline, flying from San Francisco to Moscow is cheapest
20
20
21
21
Edges can also be *directed*. So far the edges you've seen have been undirected, so if Ada knows Charles, then Charles also knows Ada. A directed edge, on the other hand, implies a one-way relationship. A directed edge from vertex X to vertex Y connects X to Y, but *not* Y to X.
22
22
23
-
Continuing from the flights example, a directed edge from San Francisco to Juneau in Alaska would indicate that there is a flight from San Francisco to Juneau, but not from Juneau to San Francisco (I suppose that means you're walking back!).
23
+
Continuing from the flights example, a directed edge from San Francisco to Juneau in Alaska would indicate that there is a flight from San Francisco to Juneau, but not from Juneau to San Francisco (I suppose that means you're walking back).
24
24
25
25

26
26
@@ -68,15 +68,15 @@ Another common graph that's used by programmers is the state machine, where edge
68
68
69
69

70
70
71
-
Graphs are awesome. Facebook made a fortune from them. If you're going to learn any data structure, it should be the graph, and the vast collection of standard graph algorithms.
71
+
Graphs are awesome. Facebook made a fortune from their social graph. If you're going to learn any data structure, it should be the graph and the vast collection of standard graph algorithms.
72
72
73
73
## Vertices and edges, oh my!
74
74
75
75
In theory, a graph is just a bunch of vertex objects and a bunch of edge objects. But how do you describe these in code?
76
76
77
-
There are two main strategies:
77
+
There are two main strategies: adjacency list and adjacency matrix.
78
78
79
-
**Adjacency List.** In an adjacency list implementation, each vertex stores a list of edges that originate from the vertex. For example, if vertex A has an edge to vertices B, C, and D, then vertex A would have a list containing 3 edges.
79
+
**Adjacency List.** In an adjacency list implementation, each vertex stores a list of edges that originate from that vertex. For example, if vertex A has an edge to vertices B, C, and D, then vertex A would have a list containing 3 edges.
80
80
81
81

82
82
@@ -97,7 +97,7 @@ Let *V* be the number of vertices in the graph, and *E* the number of edges. Th
97
97
| Add Edge | O(1) | O(1) |
98
98
| Check Adjacency | O(V) | O(1) |
99
99
100
-
"Checking adjacency" means that we try to determine that a given vertex is an immediate neighbor of another vertex. The time to check adjacency for an adjacency list is **O(V)**, because at worst a vertex is connected to *every* other vertex.
100
+
"Checking adjacency" means that we try to determine that a given vertex is an immediate neighbor of another vertex. The time to check adjacency for an adjacency list is **O(V)**, because in the worst case a vertex is connected to *every* other vertex.
101
101
102
102
In the case of a *sparse* graph, where each vertex is connected to only a handful of other vertices, an adjacency list is the best way to store the edges. If the graph is *dense*, where each vertex is connected to most of the other vertices, then a matrix is preferred.
>**Note:** There are many, many ways to implement graphs. The code given here is just one possible implementation. You probably want to tailor the graph code foreach individual problem you're trying to solve. For instance, your edges may not need a `weight` property, or you may not have the need to distinguish between directed and undirected edges.
188
+
>**Note:** There are many, many ways to implement graphs. The code given here is just one possible implementation. You probably want to tailor the graph code toeach individual problem you're trying to solve. For instance, your edges may not need a `weight` property, or you may not have the need to distinguish between directed and undirected edges.
0 commit comments