Data Structures: Graphs I
Tags: webdev, programming, computerscience, algorithms
Published: 2022-06-05 10:07:42

This is a 2-part series on graph data structures. In this first part we are going to learn about the various parts and terminologies used in graph, we will look at the types, uses and applications of graphs. While in [part 2](https://dev.to/m13ha/data-structures-graphs-ii-11i9) we shall look at graph representation and build a graph in Javascript code.


![Let's do this](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/080fumsp43ati7bs1wtc.gif)

&nbsp;

## Definition

A graph is a non-linear data structure made of nodes (vertices) and edges that connect these nodes to simulate a network. Graphs are used to show the relationship between sets of data.

&nbsp;

## Terminologies

The following are some of the commonly used terms in graph data structures:

![Graph Vertices And Vertex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyrjtv3uq819kambmc5c.png)

**Vertex:** This refers to a singular node in a graph; a vertex can be any form of data; a name, number, or co-ordinate. **[M,A,Q,Z,N]** are vertices. The plural for vertex is vertices. 

&nbsp;

![Graph with edge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjv8qcb5xngesjhfwcps.png)

**Edges:** An edge is the line connecting 2 nodes; edges have a start vertex and an end vertex. There are 2 main types of edges directed and undirected. 

&nbsp;

![Directed and Undirected Edges](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i36y4p6rpbswv44590qt.png)

A direct edge is only accessible from the start vertex and is not acknowledged by the end vertex. An undirected edge is accessible and acknowledged by both the start and end vertices.

&nbsp;

![Weighted Graph Weighted Edges](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5atuaz6f660w9bknlsgu.png)


**Weighted Edge:** A weighted edge is an edge that has a value/cost tied to it. They are used to convey the value of the relationship between vertices in a graph.

&nbsp;

![Graph Degree Vertex Degree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vkoebt8gxg5czl3ssac.png)

**Degree:** A degree refers to the total number of edges (directed or undirected) connected to a vertex. In the above image we can see that the degree of vertex **{Q}** is 4. 

&nbsp;

![Indegree Graph Outdegree Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpuev0ysjcib94zvdvqp.png)

**Indegree:** This refers to the number of incoming directed edges of a vertex. In the above image we can see that the Indegree of vertex **{B}** is 2.

**Outdegree:** This refers to the number of out-going directed  edges of a vertex. In the above image we can see that the outdegree of vertex **{V}** is 3.


&nbsp;

![Graph data structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxsbwarvb2vhpvvf7ro4.png)


**Adjacency:** Vertices that are connected by an edge are said to be adjacent (next) to one another. In the above image vertex **{A}** is adjacent to **{Q}**,  vertex **{M}** is not adjacent to **{Z}**.

&nbsp;

## Types Of Graph Data Structure

![Undirected Graph, Bi-directional graph, diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7htkrff2kxbjl7utqyfy.png)

**Undirected Graph:** An undirected graph uses undirected edges to connect its vertices. Connections in undirected graphs are acknowledged by both vertices. An undirected graph can be traversed in any direction, meaning vertex **{X}** can lead to vertex **{T}** and vice versa.

&nbsp;

![Directed Graph, Digraph, Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tp84778n9qz7mxn970ah.png)

**Directed Graph:** A directed graph uses directed edges to connect its vertices. Connections in directed graphs are one-sided and are only acknowledged by the starting vertex. Directed graphs can be traversed in a forward direction; meaning vertex **{Q}** can lead to vertex **{N}** but the reverse is not possible.

&nbsp;
![Weighted Graph Diagram, Bi-graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azoqzyrt2eprwn294ffm.png)

**Weighted Graph:** A weighted graph uses weighted edges to connect its vertices. Connections in a weighted graph are given a value/cost to convey a level of importance. Weighted graphs can be directed or undirected.

&nbsp;

## Graph Traversal

There are 2 main graph traversal algorithms Breadth-first-traversal and Depth-first-Traversal. These traversal methods are also used in [tree data structures ](https://dev.to/m13ha/data-structures-trees-i-gno); trees are considered a form of graph data structure. You can read about these traversal methods in my previous article [here](https://dev.to/m13ha/data-structures-trees-ii-4kng).

&nbsp;

## Uses And Real-life Application Of Graph Data Structure

&nbsp;
![Social Media Graph, Undirected Friendship Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa9zbfs1qcd80n5pfjno.png)

- Undirected Graphs are used to represent networks, and are used on social media platforms such as Facebook to represent connections like friendships.

&nbsp;
![directed Webpage Graph, Webpage graph diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tp6gk96gwnhy3ki7r33.png)

- Webpages operate like a directed graph in which links are the edges/connections between pages. A link leading from one webpage to another is a type of directed edge because the other page will not have a link leading to the origin page.


![Map Graph, Map Weighted Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ok6jurybajlx2kiaitf.png)

- Weighted Graphs can be used to represent maps, which is how google maps works, by using weighted graphs and setting the value of the edges to be the distance between locations.

&nbsp;

## Conclusion

In my next article we are going to look at graph representation, and implement a graph in Javascript. 

[Part 2](https://dev.to/m13ha/data-structures-graphs-ii-11i9)

![See you soon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6x4brc61bum6iyharxvt.gif)