Skip to content

Commit 4d3e08d

Browse files
Merge branch 'master' into master
2 parents 5be1d2c + 06d6e21 commit 4d3e08d

File tree

1 file changed

+42
-36
lines changed
  • src/main/java/com/thealgorithms/datastructures/graphs

1 file changed

+42
-36
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/README.md

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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 :
1+
## Graphs
2+
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:
43

54
```
65
V = a set of vertices
@@ -9,59 +8,65 @@ E = a set of edges
98

109
Each edge in `E` connects any two vertices from `V`. Based on the type of edge, graphs can be of two types:
1110

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.
11+
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`. An example of a directed edge graph is the **follow** feature of social media. If you follow a celebrity, it doesn't imply that they follow you.
1412

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.
13+
2. **Undirected**: The edges don't have any direction. So if `A` and `B` are connected, we can assume that there is an edge from both `A` to `B` and `B` to `A`. For example, in a social media graph, if two persons are friends, it implies that both are friends with each other.
1714

1815
### Components of a Graph
1916

2017
**Vertices:** Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes. Every node/vertex can be labeled or unlabelled.
21-
**Edges:** Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be labeled/unlabelled.
2218

23-
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linkedIn, Facebook. For example, in Facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender, locale etc.
19+
**Edges:** Edges are used to connect two nodes of the graph. They can be an ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be labeled/unlabeled.
20+
21+
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The networks may include paths in a city, telephone network, or circuit network. Graphs are also used in social networks like LinkedIn, Facebook. For example, on Facebook, each person is represented with a vertex (or node). Each node is a structure and contains information like person id, name, gender, locale, etc.
2422

25-
### Graph Representation:
23+
### Graph Representation
2624

2725
Graph can be represented in the following ways:
2826

2927
**Set Representation:** Set representation of a graph involves two sets: Set of vertices V = {V1, V2, V3, V4} and set of edges E = {{V1, V2}, {V2, V3}, {V3, V4}, {V4, V1}}. This representation is efficient for memory but does not allow parallel edges.
30-
**Sequential Representation:** This representation of a graph can be represented by means of matrices: Adjacency Matrix, Incidence matrix and Path matrix.
31-
**Adjacency Matrix:** This matrix includes information about the adjacent nodes. Here, aij = 1 if there is an edge from Vi to Vj otherwise 0. It is a matrix of order V×V.
32-
**Incidence Matrix:** This matrix includes information about the incidence of edges on the nodes. Here, aij = 1 if the jth edge Ej is incident on ith vertex Vi otherwise 0. It is a matrix of order V×E.
33-
**Path Matrix:** This matrix includes information about the simple path between two vertices. Here, Pij = 1 if there is a path from Vi to Vj otherwise 0. It is also called as reachability matrix of graph G.
34-
**Linked Representation:** This representation gives the information about the nodes to which a specific node is connected i.e. adjacency lists. This representation gives the adjacency lists of the vertices with the help of array and linked lists. In the adjacency lists, the vertices which are connected with the specific vertex are arranged in the form of lists which is connected to that vertex.
3528

29+
**Sequential Representation:** This representation of a graph can be represented by means of matrices: Adjacency Matrix, Incidence matrix, and Path matrix.
30+
31+
**Adjacency Matrix:** This matrix includes information about the adjacent nodes. Here, aij = 1 if there is an edge from Vi to Vj; otherwise, it's 0. It is a matrix of order V×V.
32+
33+
**Incidence Matrix:** This matrix includes information about the incidence of edges on the nodes. Here, aij = 1 if the jth edge Ej is incident on the ith vertex Vi; otherwise, it's 0. It is a matrix of order V×E.
34+
35+
**Path Matrix:** This matrix includes information about the simple path between two vertices. Here, Pij = 1 if there is a path from Vi to Vj; otherwise, it's 0. It is also called the reachability matrix of graph G.
36+
37+
**Linked Representation:** This representation gives information about the nodes to which a specific node is connected, i.e., adjacency lists. This representation gives the adjacency lists of the vertices with the help of arrays and linked lists. In the adjacency lists, the vertices connected to the specific vertex are arranged in the form of lists that are connected to that vertex.
3638

37-
### Real-Time Applications of Graph:
38-
Graphs are used to represent flow of control in computers.
39-
Graphs are used in social networking sites where users act as nodes and connection between them acts as edges.
39+
### Real-Time Applications of Graph
40+
41+
Graphs are used to represent the flow of control in computers.
42+
Graphs are used in social networking sites where users act as nodes, and connections between them act as edges.
4043
In an operating system, graphs are used as resource allocation graphs.
41-
Graphs are used in Google maps to find the shortest route.
42-
Graphs are also used in airlines system for effective route optimization.
43-
In-state transition diagrams, the graph is used to represent their states and their transition.
44+
Graphs are used in Google Maps to find the shortest route.
45+
Graphs are also used in the airline system for effective route optimization.
46+
In state transition diagrams, the graph is used to represent states and their transitions.
4447
In transportation, graphs are used to find the shortest path.
4548
In circuits, graphs can be used to represent circuit points as nodes and wires as edges.
4649
Graphs are used in solving puzzles with only one solution, such as mazes.
47-
Graphs are used in computer networks for Peer to peer (P2P) applications.
48-
Graphs basically in the form of DAG(Directed acyclic graph) are used as alternative to blockchain for cryptocurrency. For example crypto like IOTA, Nano are mainly based on DAG.
50+
Graphs are used in computer networks for Peer to Peer (P2P) applications.
51+
Graphs, basically in the form of DAG (Directed Acyclic Graph), are used as an alternative to blockchain for cryptocurrency. For example, cryptocurrencies like IOTA and Nano are mainly based on DAG.
52+
53+
### Advantages of Graph
4954

50-
### Advantages of Graph:
51-
By using graphs we can easily find the shortest path, neighbors of the nodes, and many more.
55+
Using graphs, we can easily find the shortest path, neighbors of the nodes, and many more.
5256
Graphs are used to implement algorithms like DFS and BFS.
53-
It is used to find minimum spanning tree which has many practical applications.
54-
It helps in organizing data.
55-
Because of its non-linear structure, helps in understanding complex problems and their visualization.
57+
They are used to find minimum spanning trees, which have many practical applications.
58+
Graphs help in organizing data.
59+
Because of their non-linear structure, graphs help in understanding complex problems and their visualization.
60+
61+
### Disadvantages of Graph
5662

57-
### Disadvantages of Graph:
58-
Graphs use lots of pointers which can be complex to handle.
59-
It can have large memory complexity.
60-
If the graph is represented with an adjacency matrix then it does not allow parallel edges and multiplication of the graph is also difficult.
63+
Graphs use lots of pointers, which can be complex to handle.
64+
They can have large memory complexity.
65+
If the graph is represented with an adjacency matrix, then it does not allow parallel edges, and multiplication of the graph is also difficult.
6166

6267
### Representation
6368

64-
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.
69+
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 edges to 2, 3, and 6, the list corresponding to 1 will have 2, 3, and 6 as entries. Consider the following graph:
6570

6671
```
6772
0: 1-->2-->3
@@ -70,9 +75,10 @@ If the graph is represented with an adjacency matrix then it does not allow para
7075
3: 0-->4
7176
4: 3
7277
```
73-
It means there are edges from 0 to 1, 2 and 3; from 1 to 0 and 2 and so on.
74-
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.
75-
The mtrix for the above graph:
78+
79+
It means there are edges from 0 to 1, 2, and 3; from 1 to 0 and 2, and so on.
80+
81+
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. The matrix for the above graph:
7682

7783
```
7884
0 1 2 3 4

0 commit comments

Comments
 (0)