|
1 |
| - |
2 |
| - |
3 |
| -= Graph Data Structure |
4 |
| - |
5 |
| -Graphs is one of my favorite data structures. They have a lot of cool |
6 |
| -applications and are used in more places than you can imagine. First, |
7 |
| -let’s start with the basics. |
8 |
| - |
9 |
| -A graph is a non-linear data structure where a node can have zero or |
10 |
| -more linked nodes. |
11 |
| - |
12 |
| -You can think of graph like an extension of a Linked List. Instead of |
13 |
| -having only a next or previous linked node, you can have as many as you |
14 |
| -want. Actually, you can an array of linked nodes. |
15 |
| - |
16 |
| -/** |
17 |
| - |
18 |
| -* Graph node/vertex that hold adjacencies nodes |
19 |
| - |
20 |
| -*/ |
21 |
| - |
22 |
| -class Node \{ |
23 |
| - |
24 |
| -constructor(value) \{ |
25 |
| - |
26 |
| -this.value = value; |
27 |
| - |
28 |
| -this.adjacents = []; // adjacency list |
29 |
| - |
30 |
| -} |
31 |
| - |
32 |
| -} |
33 |
| - |
34 |
| -As you can see, it’s pretty similar to the Linked List node indeed. The |
35 |
| -only difference is that uses an *array* of the linked nodes instead. |
36 |
| - |
37 |
| -Other difference between a linked list and graph is that linked list |
38 |
| -have a start/first/root node, while the graph doesn’t. You can start |
39 |
| -traversing a graph from anywhere and there might be circular references |
40 |
| -as well. Let’s study this graph properties! |
41 |
| - |
42 |
| -== Graph Properties |
43 |
| - |
44 |
| -The connection between two nodes is called *edge*. Also, nodes might be |
45 |
| -called *vertex*. |
46 |
| - |
47 |
| -image:extracted-media/media/image42.png[image,width=305,height=233] |
48 |
| - |
49 |
| -Figure 26 - Graph is composed of vertices/nodes and edges |
50 |
| - |
51 |
| -=== Directed Graph vs Undirected |
52 |
| - |
53 |
| -A graph can *directed* and *undirected*. |
54 |
| - |
55 |
| -image:extracted-media/media/image43.jpg[image,width=469,height=192] |
56 |
| - |
57 |
| -Figure 27 - Graph: directed vs undirected |
58 |
| - |
59 |
| -A *directed graph (digraph)* has edges that are *one-way street*. E.g. |
60 |
| -On the directed example, you can only go from green node to orange and |
61 |
| -not the other way around. When one node has an edge to itself is called |
62 |
| -a *self-loop*. |
63 |
| - |
64 |
| -An *undirected graph* has edges that are *two-way street*. E.g. On the |
65 |
| -undirected example, you can traverse from the green node to the orange |
66 |
| -and vice versa. |
67 |
| - |
68 |
| -=== Graph Cycles |
69 |
| - |
70 |
| -A graph can have *cycles* or not. |
71 |
| - |
72 |
| -image:extracted-media/media/image44.jpg[image,width=444,height=194] |
73 |
| - |
74 |
| -Figure 28 - Cyclic vs Acyclic Graphs. |
75 |
| - |
76 |
| -A *cyclic graph* is the one that you can pass through a node more than. |
77 |
| -E.g. On the cyclic illustration, if you start in the green node, then go |
78 |
| -the orange and purple, finally, you could come back to green again. |
79 |
| -Thus, it has a *cycle*. |
80 |
| - |
81 |
| -All *undirected* graphs are cyclic but not all *directed* graphs are |
82 |
| -cyclic. |
83 |
| - |
84 |
| -An acyclic graph is the one that you can’t pass through a node more than |
85 |
| -once. E.g. in the acyclic illustration, can you to find a path where you |
86 |
| -can pass through the same vertex more than one? |
87 |
| - |
88 |
| -*Directed Acyclic Graphs (DAG)* are also known as a *Tree* when each |
89 |
| -node has only *one parent*. |
90 |
| - |
91 |
| -=== Connected vs Disconnected vs Complete Graphs |
92 |
| - |
93 |
| -image:extracted-media/media/image45.emf[image,width=528,height=176] |
94 |
| - |
95 |
| -Figure 29 - Different kinds of graphs: disconnected, connected, and |
96 |
| -complete. |
97 |
| - |
98 |
| -A *disconnected graph* is one that have one or more subgraph. In other |
99 |
| -words, a graph is *disconnected* if there are two nodes that doesn’t |
100 |
| -have a path between them. |
101 |
| - |
102 |
| -A *connected graph* is the opposite to disconnected, there’s a path |
103 |
| -between every node. |
104 |
| - |
105 |
| -A *complete graph* is where every node is adjacent to all the other |
106 |
| -nodes in the graph. E.g. If there are 7 nodes, every node has 6 edges. |
107 |
| - |
108 |
| -=== Weighted Graphs |
109 |
| - |
110 |
| -Weighted graphs have labels in the edges. The label is called *weight* |
111 |
| -or *cost*. The weight can represent many things like distance, travel |
112 |
| -time, or anything else. |
113 |
| - |
114 |
| -image:extracted-media/media/image46.png[image,width=528,height=337] |
115 |
| - |
116 |
| -Figure 30 - Weighted Graph representing USA airports distance in miles. |
117 |
| - |
118 |
| -For instance, a weighted graph can have the distance between nodes. So, |
119 |
| -algorithms can use the weight and optimize the path between them. |
120 |
| - |
121 |
| -== Exciting Graph applications in real-world |
122 |
| - |
123 |
| -Now that we know what graphs are and some of their properties let’s |
124 |
| -discuss about some real-life usages of graphs. |
125 |
| - |
126 |
| -Graphs become a metaphor where nodes and edges model something from our |
127 |
| -physical world. Just to name a few: |
128 |
| - |
129 |
| -* Optimizing Plane traveling |
130 |
| - |
131 |
| -* Nodes = Airport |
132 |
| -* Edges = Direct flights between two airports |
133 |
| -* Weight = miles between airports | cost | time |
134 |
| - |
135 |
| -* GPS Navigation System |
136 |
| - |
137 |
| -* Node = road intersection |
138 |
| -* Edge = road |
139 |
| -* Weight = time between intersections |
140 |
| - |
141 |
| -* Network routing |
142 |
| - |
143 |
| -* Node = server |
144 |
| -* Edge = data link |
145 |
| -* Weight = connection speed |
146 |
| - |
147 |
| -There are endless applications for graphs in electronics, social |
148 |
| -networks, recommendation systems and many more. That’s cool and all, but |
149 |
| -how do we represent graphs in code? Let’s see that in the next section. |
150 |
| - |
151 |
| -== Representing Graphs |
152 |
| - |
153 |
| -There are two main ways to graphs one is: |
154 |
| - |
155 |
| -* Adjacency Matrix |
156 |
| -* Adjacency List |
157 |
| - |
158 |
| -=== Adjacency Matrix |
159 |
| - |
160 |
| -Representing graphs as adjacency matrix is done using a two-dimensional |
161 |
| -array. For instance, let’s say we have the following graph: |
162 |
| - |
163 |
| -image:extracted-media/media/image47.png[image,width=438,height=253] |
164 |
| - |
165 |
| -Figure 31 - Graph and its adjacency matrix. |
166 |
| - |
167 |
| -The size of the matrix is given by the number of vertices |V|, in the |
168 |
| -example we have 5 vertices so we have a 5x5 matrix. |
169 |
| - |
170 |
| -To fill up the matrix, we go row by row. Mark with 1 (or any other |
171 |
| -weight) when you find an edge. E.g. |
172 |
| - |
173 |
| -* Row 0: It has a self-loop, so it has a 1 in the intersection of 0,0. |
174 |
| -The node 0 also has an edge to 1 and 4 so we mark it. |
175 |
| -* Row 1: The node 1 has one edge to 3 so we mark it. |
176 |
| -* Row 2: Node 2 goes to Node 4, so we mark the insertion with 1. |
177 |
| -* And so on… |
178 |
| - |
179 |
| -The example graph above is a directed graph (digraph). In case of |
180 |
| -undirected graph, the matrix would be symmetrical by the diagonal. |
181 |
| - |
182 |
| -If we represent the example graph in code, it would be something like |
183 |
| -this: |
184 |
| - |
185 |
| -_const_ digraph = [ |
186 |
| - |
187 |
| -[1, 1, 0, 0, 1], |
188 |
| - |
189 |
| -[0, 0, 0, 1, 0], |
190 |
| - |
191 |
| -[0, 0, 0, 0, 1], |
192 |
| - |
193 |
| -[0, 0, 1, 0, 0], |
194 |
| - |
195 |
| -[0, 1, 0, 0, 0], |
196 |
| - |
197 |
| -]; |
198 |
| - |
199 |
| -It would be very easy to tell if two nodes are connected. Let’s query if |
200 |
| -node 2 is connected to 3: |
201 |
| - |
202 |
| -digraph[2][3]; _//=> 0_ |
203 |
| - |
204 |
| -digraph[3][2]; _//=> 1_ |
205 |
| - |
206 |
| -As you can see we don’t have a link from node 2 to 3, but we do in the |
207 |
| -opposite direction. Querying arrays is constant time *O(1)*, so no bad |
208 |
| -at all. |
209 |
| - |
210 |
| -The issue with the adjacency matrix is the space it takes. Let’s say you |
211 |
| -want to represent the entire Facebook network on a digraph. You would |
212 |
| -have a huge matrix of 1.2 billion x 1.2 billion. The worst part is that |
213 |
| -most of it would be empty (zeros) since people are connected to at most |
214 |
| -few thousands. |
215 |
| - |
216 |
| -When the graph has few connections compared to the number of nodes we |
217 |
| -say that we have a *sparse graph*. On the opposite, if we have almost |
218 |
| -complete graphs we say we have a *dense graph*. |
219 |
| - |
220 |
| -The space complexity of adjacency matrix is *O(|V|^2^)*, where |V| is |
221 |
| -the number of vertices/nodes. |
222 |
| - |
223 |
| -=== Adjacency List |
224 |
| - |
225 |
| -Another way to represent a graph is using an adjacency list. This time |
226 |
| -instead of using an array (matrix) we use a list. |
227 |
| - |
228 |
| -image:extracted-media/media/image48.png[image,width=528,height=237] |
229 |
| - |
230 |
| -Figure 32 – Graph represented as an Adjacency List. |
231 |
| - |
232 |
| -Body |
233 |
| - |
234 |
| -== Adding a vertex |
235 |
| - |
236 |
| -Body text |
237 |
| - |
238 |
| -== Adding an edge |
239 |
| - |
240 |
| -Body text |
241 |
| - |
242 |
| -== Querying Adjacency |
243 |
| - |
244 |
| -Body text |
245 |
| - |
246 |
| -== Deleting a vertex |
247 |
| - |
248 |
| -Body text |
249 |
| - |
250 |
| -== Deleting an edge |
251 |
| - |
252 |
| -Body text |
253 |
| - |
254 |
| -== Graph Complexity |
255 |
| - |
256 |
| -Graph search has its own chapter |
257 |
| - |
258 |
| -= Summary |
259 |
| - |
260 |
| -Body text |
261 |
| - |
262 |
| -4 |
263 |
| - |
264 | 1 | [[_Toc525822218]]Learning Fast Sorting Algorithms
|
265 | 2 |
|
266 | 3 | Introduction.
|
|
0 commit comments