Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf7cf93

Browse files
committed
feat(add chapters to , -algorithms and )
1 parent 129fca3 commit cf7cf93

22 files changed

Lines changed: 387 additions & 2 deletions

graph-algorithms/minimum-cut.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Graph Minimum Cut
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Minimum Spanning Tree
2+
3+
In electrical circuit design, usually there are several
4+
5+
## Kruskal's Algorithm
6+
7+
## Prim's Algorithm

graph-algorithms/overview.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Graph Algorithms
2+
3+
[Graph Theory](https://en.wikipedia.org/wiki/Graph_theory) is a study of _graphs_, a mathematical structure modeling the associations between objects. It is vital for many computer science problems and worth attention for not only theoretical studies but also practical application development.
4+
5+
This chapter discusses fundamental graph structures, typical graph problems and correlated algorithm analysis. Specifically, graph searching primitives such as DFS and BFS are the most important techniques throughout the graph algorithms.
6+
7+
## Graph Definitions
8+
9+
Generally, there are two kinds of graphs: [directed graph](#directed-graph) and [undirected graph](#undirected-graph). Both of them involve a relationship between **Vertex** _V_ and **Edge** _E_.
10+
11+
### Directed Graph
12+
13+
Given a graph _G_ = (_V_, _E_), wherein the _V_ is a set of vertices of _G_ and _E_ is a set of edges going in and out from vertices with directions, connected them together. Specifically, given vertices {1, 2, 3, 4, 5, 6}, a graph can be built like below:
14+
15+
<figure style="text-align:center">
16+
<img src="../images/directed-graph.png" />
17+
<figcaption>Figure 1. Directed Graph</figcaption>
18+
</figure>
19+
20+
_Note: there are cycles (even self cycles, a vertex points to itself) in directed graphs and to be noted that problems like [topological sort](../sorting/topological-sort.md) could not have solutions if cycles present, the directed graph with no cycles is called DAG (directed acyclic graph)_.
21+
22+
### Undirected Graph
23+
24+
Unlike [directed graph](#directed-graph), undirected graph only has edges with no directions between connected vertices. Since the relations among vertices have no ordering, there exists no self cycles. Similarly,
25+
26+
<figure style="text-align:center">
27+
<img src="../images/undirected-graph.png" />
28+
<figcaption>Figure 2. Undirected Graph</figcaption>
29+
</figure>
30+
31+
### Degrees
32+
33+
In an [undirected graph](#undirected-graph), the degree of a vertex means the number of edges associated with that vertex. For instance, the degree of vertex 3 in the above graph is 3;
34+
35+
While in the [directed graph](#directed-graph), the degree of a vertex is the summation of its **in-degree** and **out-degree**. Such **in-degree** means the number of edges pointing towards the vertex and **out-degree** means the number of edges pointing out of the vertex.
36+
37+
_Note: the vertex with zero degree means it is a lonely island, disconnected from any other vertices in the graph_.
38+
39+
### Connectivity
40+
41+
If for any given vertices in an [undirected graph](#undirected-graph), there exists paths reachable from all other vertices, then the graph is _connected_. Specifically, if an undirected graph _G_ = (_V_, _E_) is connected, the number of edges is larger than or equal to number of vertices minus 1: |_E_| &ge; |_V_| - 1.
42+
43+
There are only definitions of **strongly connected** and **weakly connected** in a [directed graph](#directed-graph). In a **strongly connected** directed graph _G_ = (_V_, _E_), for any two distinct vertices x, y within _V_, there exists paths from x to y and from y to x;
44+
45+
Substituting all directional edges of a directed graph with undirected ones could obtain a _base graph_ of the original graph. If such a _base graph_ is **connected**, then the original directed graph is **weakly connected**.
46+
47+
## Graph Representations
48+
49+
Two vertices sharing a same edge are _adjacent_ in a graph. To define such relationships among vertices, two practical uses of data structures are introduced: [adjacency matrix](#adjacency-matrix) and [adjacency list](#adjacency-list).
50+
51+
### Adjacency Matrix
52+
53+
This matrix (e.g. _adj[][]_) is a 2D array with size |V| &cross; |V|; In an undirected graph, if vertex _a_ and vertex _b_ are connected, then adj[a][b] = 1 and adj[b][a] = 1; if there are no edges between say vertex _e_ and vertex _c_, then adj[e][c] = 0, adj[c][e] = 0;
54+
55+
For a directed graph, the matrix cell value depends on the existance of directional edges; If vertex _a_ has an edge going outward to vertex _b_ while vertex _b_ has no edges going inward to vertex _a_, then adj[a][b] = 1 but adj[b][a] = 0
56+
57+
<figure style="text-align:center">
58+
<img src="../images/adjacency-matrix.jpg" />
59+
<figcaption>Figure 3. Adjacency Matrices of a Directed Graph and an Undirected Graph</figcaption>
60+
</figure>
61+
62+
Adjacency matrix is practical when the graph is **dense** (at least one edge going in between two vertices) and finding the existence of edges is immediate (LOOKUP costs &Omicron;(1)). It is less so if the graph is sparse (the graph topology structure approaches a _**forest**_ rather than a graph).
63+
64+
### Adjacency List
65+
66+
Adjacency list is an array of linkedlist in design and it costs only &Omicron;(|V| + |E|) compared to [adjacency matrix](#adjacency-matrix). In addition, the array can be dynamic which means adding vertices to a graph is easier than a static structure like adjacency matrix.
67+
68+
<figure style="text-align:center">
69+
<img src="../images/adjacency-list.png" />
70+
</figcaption>Figure 4. Adjacency List of a Directed Graph</figcaption>
71+
</figure>
72+
73+
Though it normally costs &Omicron;(|E|) time to LOOKUP edge associations, it is a preferable choice than adjacency matrix in a massive-scale data network such as WEB.
74+
75+
## Table of Contents
76+
77+
* [Breadth-First Search](../searching/graph-search.md) - a searching primitive prototype for many important algorithms like [Minimum Spanning Tree] and [Dijkstra Shortest Path].
78+
* [Depth-First Search](../searching/graph-search.md) - in contrast to BFS, DFS provides a simple recursive implementation.
79+
* [Shortest Path Algorithms](shortest-path.md) - a prevalent type of graph algorithm in practice, e.g. find a shortest path from location A to location B in map.
80+
* [Minimum Spanning Tree](minimum-spanning-tree.md) - a technique to transform graphs to trees
81+
* [Graph Minimum-Cut](minimum-cut.md) - split the graph into different groups for applications like [community finding](https://en.wikipedia.org/wiki/Community_structure#Algorithms_for_finding_communities).
82+
* [Topological Sort](topological-sort.md) - sort a [DAG](#directed-graph) graph with respect to its topological relations.

graph-algorithms/shortest-path.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Shortest Path
2+
3+
In a [Geo-spatial Network](https://en.wikipedia.org/wiki/Spatial_network) application, it is a common feature to compute shortest paths for various purposes, e.g. planning a route from current location to a destination on the geographic map; scheduling nearest [Uber](https://en.wikipedia.org/wiki/Uber_(company)) cars to users requesting ride service.
4+
5+
Specifically, there are two types of tasks to compute shortest paths: [single-source](#single-source) and [multi-source](#multi-source), both of which are computed based on **weighted graph** structures. One of the most important algorithms in the 20th is [Dijkstra's Algorithm](#dijkstras-algorithm).
6+
7+
## Generality
8+
9+
a path from &vscr;<sub>0</sub> -> &vscr;<sub>k</sub> is defined by _p_ (&vscr;<sub>0</sub> -> &vscr;<sub>0</sub> is 0), and the shortest path from source &uscr; to &vscr; is:
10+
11+
<figure style="text-align:center">
12+
<img src="../images/shortest-path.png" />
13+
<figcaption>Figure 1. A Delta Function Denotes the Shortest Path Total Value</figcaption>
14+
</figure>
15+
16+
## Single-Source
17+
18+
Given weighted graph _G_ = (_V_, _E_), weight w(u, v) of two vertices (u, v) and a source _S_, compute &delta;(_S_, _v_<sub>i</sub>) wherein _v_<sub>i</sub> is a vertex from vertices set _V_.
19+
20+
<figure style="text-align:center">
21+
<img src="../images/shortest-path-2.png" />
22+
<figcaption>Figure 2. Auxiliary Static Data Structure to Store Shortest Paths</figcaption>
23+
</figure>
24+
25+
Mathematically, use the above structure to initialize to infinity and converge to the shortest paths values from a given source _S_ would yield the final solution at hand.
26+
27+
In addition, define another array &pi;<sub>i</sub> (i=1,2,...,n) to track the **predecessor** of all vertices, where &pi;[s] = s in order to output the shortest paths.
28+
29+
_Note: the convergence step is formally termed as **Relaxation**, which would be covered in Dijkstra's Algorithm discussion_.
30+
31+
The classical [Dijkstra's Algorithm](#dijkstras-algorithm) and [Bellman-Ford algorithm](#bellman-ford-algorithm) are further discussed based on the above assumptions.
32+
33+
### Dijkstra's Algorithm
34+
35+
Invented by Turing Award winner [Edsger W. Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra), this algorithm adopts a [greedy](../greedy-algorithms) approach to search for the shortest path among all the paths leading from a single source to the destination.
36+
37+
#### Fundamental Ideas
38+
39+
It is applicable for both directed and undirected graphs, in which the edges must have non-negative weights. In the original version of Dijkstra's algorithm, it only tries to find the minimal path between two nodes, but more commonly the modern variant can find the shortest paths from a single source
40+
to all other nodes, producing a shortest path tree.
41+
42+
Specifically, for each edge (_u_, _v_) in a **non-negative** weighted graph _G_, maintain a set _S_ of vertices whose final shortest paths have been computed. Repeatedly select &uscr; out of _V_ - _S_ with minimum shortest path estimate, add &uscr; to _S_
43+
and relax all edges adjacent and reachable from &uscr;.
44+
45+
<pre>
46+
<code>
47+
DIJKSTRA(graph, source)
48+
Initialize d array to either Infinity or 0 (s = source)
49+
Initialize &pi; array of vertices with their self
50+
set S := empty
51+
Q := a priority queue of reachable vertices from source
52+
while Q != empty
53+
u = EXTRACT_MIN(Q) // deletes a vertex with minimal distance to the source
54+
S.add(u)
55+
for vertex v adjacent to u
56+
RELAX(u, v, d, &pi;)
57+
58+
output array d and &pi;
59+
60+
RELAX(u, v, d, &pi;)
61+
if d[v] > d[u] + w(u, v)
62+
d[v] = d[u] + w(u, v)
63+
&pi;[v] = u;
64+
</code>
65+
</pre>
66+
67+
<figure style="text-align:center">
68+
<img src="../images/shortest-path-3.png" />
69+
<figcaption>Figure 3. An Example of Dijkstra's Algorithm</figcaption>
70+
</figure>
71+
72+
### Bellman-Ford Algorithm
73+
74+
Though slower than peer shortest path algorithm like Dijkstra's, Bellman-Ford algorithm can compute the shortest paths from a single source that might bear negative edges in between.
75+
76+
Specifically, given a graph _G_ = (_V_, _E_), this algorithm answers true if a shortest path exists from a single source to a destination without cycles, otherwise answers false;
77+
78+
<pre>
79+
<code>
80+
BELLMAN_FORD(graph, source)
81+
Initialize d array to either Infinity or 0 (s = source)
82+
Initialize &pi; array of vertices with their self
83+
for i := 1 to |V| - 1
84+
for each edge (u, v) &isin; E
85+
RELAX(u, v, d, &pi;) // same sub-routine as Dijkstra
86+
for edge (u, v) &isin; E
87+
if d[v] > d[u] + w(u, v)
88+
report a negative cycle exists, return false
89+
return true
90+
</code>
91+
</pre>
92+
93+
_Note: why do we care about negative edges? reasons include computational tasks in a water pipeline or electric wires, or financial models with various cost and revenue gains in many vertices_.
94+
95+
## Additional References
96+
97+
1. Shortest Paths II, MIT OpenCourseware, https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec16.pdf
98+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Topological Sort
2+
3+
A topological sort, a.k.a Topo-sort, is performed on DAG ([Directed Acyclic Graph](../graph-algorithms/overview.md)) to compute a **linear ordering** of all the vertices in graph. It satisfies the following condition: if a graph _G_ contains an edge (_u_, _v_), then the vertex _u_ must appear before the vertex _v_ in the sorted ordering.
4+
5+
Unlike the general discussions in [sorting](overview.md) section, Topo-sort tries to layout a graph structure into a flatten distribution of vertices. This method is widely adopted in the industry, e.g. a manufacturing process of specific products might have numerous small procedures combined together and to compute a topological ordering of such procedures are critical for pipelining the manufacturing process as to reduce time & money cost.
6+
7+
There are many algorithms that can achieve the goal, here a version of Kahn's algorithm and [BFS](../searching/graph-search.md) are discussed.
8+
9+
## Kahn's Algorithm
10+
11+
12+
13+
## BFS approach
14+

images/adjacency-list.png

36.9 KB
Loading

images/adjacency-matrix.jpg

38.7 KB
Loading

images/bfs1.png

35.8 KB
Loading

images/bfs2.png

20.9 KB
Loading

images/directed-graph.png

11.2 KB
Loading

0 commit comments

Comments
 (0)