Skip to content

Commit 4b9acae

Browse files
authored
Add README for Graph (TheAlgorithms#2601)
1 parent 793bedb commit 4b9acae

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

DataStructures/Graphs/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Graph
2+
3+
Graph is a useful data structure for representing most of the real world problems involving a set of users/candidates/nodes and their relations. A Graph consists of two parameters :
4+
5+
```
6+
V = a set of vertices
7+
E = a set of edges
8+
```
9+
10+
Each edge in `E` connects any two vertices from `V`. Based on the type of edge, graphs can be of two types:
11+
12+
1. **Directed**: The edges are directed in nature which means that when there is an edge from node `A` to `B`, it does not imply that there is an edge from `B` to `A`.
13+
An example of directed edge graph the **follow** feature of social media. If you follow a celebrity, it doesn't imply that s/he follows you.
14+
15+
2. **Undirected**: The edges don't have any direction. So if `A` and `B` are connected, we can assume that there is edge from both `A` to `B` and `B` to `A`.
16+
Example: Social media graph, where if two persons are friend, it implies that both are friend with each other.
17+
18+
19+
### Representation
20+
21+
1. **Adjacency Lists**: Each node is represented as an entry and all the edges are represented as a list emerging from the corresponding node. So if vertex `1` has eadges to 2,3, and 6, the list corresponding to 1 will have 2,3 and 6 as entries. Consider the following graph.
22+
23+
```
24+
0: 1-->2-->3
25+
1: 0-->2
26+
2: 0-->1
27+
3: 0-->4
28+
4: 3
29+
```
30+
It means there are edges from 0 to 1, 2 and 3; from 1 to 0 and 2 and so on.
31+
2. **Adjacency Matrix**: The graph is represented as a matrix of size `|V| x |V|` and an entry 1 in cell `(i,j)` implies that there is an edge from i to j. 0 represents no edge.
32+
The mtrix for the above graph:
33+
34+
```
35+
0 1 2 3 4
36+
37+
0 0 1 1 1 0
38+
1 1 0 1 0 0
39+
2 1 1 0 0 0
40+
3 1 0 0 0 1
41+
4 0 0 0 1 0
42+
```

0 commit comments

Comments
 (0)