Docsity

Graph Data Structures: A Comprehensive Guide to Concepts and Algorithms, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Graph:
Graph is a non-linear data structure, used in varieties of applications in computer science.
Logically/or Technically:
A graph is a collection of entities called nodes.
Nodes are connected by edges.
However, a graph doesn’t have rules dictating the connection of the nodes.
i.e. A node may be connected to any other node in any possible way.
In mathematics (discrete mathematics), study of graphs in called as Graph Theory.
A mathematical definition of graph:
“A graph G is an ordered pair of a set V of vertices (nodes) and a set E of edges”.
G = (V, E)
We represent:
- Ordered Pair: As (A, B) where (A, B) is not same as (B, A); i.e. the order matters; and
- Unordered Pair: As {A, B} where {A, B} = {B, A}.
Each node in a graph has some identification, and naming doesn’t have an order.
i.e. we can give any name to any node.
Now, to represented a Graph G, we need to know the Types of Edges:
i. Directed Edge:
- Here, with a directed edge connecting two vertices ‘A’ & ‘B’, we
understand that there is a path from “Vertex A” to another “Vertex B”;
but there is not a path from Vertex B to Vertex A.
- Here, ‘A’ is the origin and ‘B’ is the destination.
- Hence, directed edge is one-way.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Graph Data Structures: A Comprehensive Guide to Concepts and Algorithms and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Graph:

 Graph is a non-linear data structure , used in varieties of applications in computer science.  Logically/or Technically:  A graph is a collection of entities called nodes.  Nodes are connected by edges.  However, a graph doesn’t have rules dictating the connection of the nodes. i.e. A node may be connected to any other node in any possible way.  In mathematics (discrete mathematics), study of graphs in called as Graph Theory.  A mathematical definition of graph : “A graph G is an ordered pair of a set V of vertices (nodes) and a set E of edges ”. G = (V, E)  We represent:

- Ordered Pair: As (A, B) where (A, B) is not same as (B, A); i.e. the order matters; and - Unordered Pair: As {A, B} where {A, B} = {B, A}.  Each node in a graph has some identification, and naming doesn’t have an order.  i.e. we can give any name to any node. Now, to represented a Graph G , we need to know the Types of Edges :

i. Directed Edge:

  • Here, with a directed edge connecting two vertices ‘ A’ & ‘ B’ , we understand that there is a path from “Vertex A” to another “ Vertex B” ; but there is not a path from Vertex B to Vertex A.
  • Here, ‘A’ is the origin and ‘B’ is the destination.
  • Hence, directed edge is one-way.
  • Un above figure, we represent a directed edge as an Ordered Pair: (A, B) // i.e. (Origin, Destination) using Normal Brackets.
  • Hence, to have an edge (B, A) we should have a directed edge from vertex ‘B’ to vertex ‘A’. ii. Undirected Edge:
  • In an Undirected Edge, the connection is Two-Way.
  • i.e. In above figure, there is a path from Vertex V1 to Vertex V2, and also Vertex V2 to Vertex V1.
  • So, the representation is Unordered.
  • Since, the edge is bi-directional, the Origin and Destination are Not Fixed.
  • Here, an undirected edge can be: {V1, V2} // Use of curly brackets. Example of Graph and its Representation:

Here, V = {A, B, C, D, E} E = { {A, B}, {A, C}, {A, D}, {B, D}, {B, E}, {C, D}, {D, E} }  Hence, typically in a graph, an edge is either directed or undirected. Some Applications of Graph: Social Networks, Maps, Path Optimization, Network Packet Traffic, Transportation Networks etc.

4. Sparse Graph:  Sparse graph is a graph in which the number of edges is close to the minimal number of edges.  i.e. Few edges between nodes.  It has number of edges closer to n.

5. Undirected Graph:

 An undirected graph is graph has vertices connected together, where all the edges are

bidirectional (i.e. connected with undirected edges).

 In an undirected graph, the order of the vertices in the pairs in the Edge set doesn't

matter.

 The adjacency relation in undirected graph is symmetric. i.e. {a, b} = {b, a}

Example:

6. Directed Graph (Digraph):

 In a directed graph, each edge is a directed edge, connecting an origin and a destination vertex.  Here, the order of vertices in the set of edges matters.

 Edges denote the path from a vertex to another vertex. Example:

 Here, (2, 4) is not same as (4, 2).

7. Simple Graph:

 A simple graph is a graph which does not contain more than one edge between the pair of

vertices.

 Hence, there are no loops and no parallel edges in a simple graph.

 Railway tracks connecting cities can be thought as simple graph.

Example:

8. Multi Graph:

 Any graph which contains some parallel edges but doesn’t contain any self-loop is called multi graph.

  • Parallel Edge: If two vertices are connected with more than one edge than such edges are called parallel edges; i.e. many routes to one destination.
  • Loop: An edge of a graph which joins a vertex to itself is called loop or a self- loop.

11. Acyclic Graph:

 An acyclic graph is a graph having no graph cycles.  An acyclic graph may be a directed or an undirected graph.  A tree is an example of an acyclic graph.  But, a directed acyclic graph may not be a tree. Example:

Here, DAG = Directed Acyclic Graph.

12. Labeled Graph:

 A labeled graph consists of assignment of labels, traditionally represented by integers, to edges and/or vertices of a graph.  The important thing to note is that the vertices can be distinguished one from another using the vertex labels.  A labeled graph can be a vertex labeled, or edge labeled or both.  Note that it is not necessary for all the vertices to be assigned different labels. Example: In the following graph, the vertices are labeled: a, b, c, d, e

13. Weighted Graph:

 A weighted graph is a graph in which each edge is given a numerical weight.  A weighted graph is therefore a special type of labeled graph in which the labels on the edges are numbers (which are usually taken to be positive).  Example, the edges of a road network can be weighted with their length, speed limit, drive-time etc. Example:

14. Un-Weighted Graph:

 An un-weighted graph is a graph in which weight of each edge is 1 unit.  i.e. there is no cost (weight) distinction between various edges. Example:

Graph Traversal:

 Graph traversal is a technique used for a visiting / or reading / or exploring each vertex in a graph exactly once.

Visited: NULL (Let’s start with vertex S)

Visited: S (Visited S, pushed it into stack and look for its unvisited adjacent node)

Visited: S, A (A is adjacent to S since S was at Top of Stack)

Visited: S, A, D (D is adjacent to A)

Visited: S, A, D, B (B is adjacent to D)

Visited: S, A, D, B, C (No unvisited adjacent vertex to B. Hence, back track to D and look for its unvisited neighbor; found C and visited). Finally, POP the stack to look for any unvisited adjacent vertex of an already visited vertex, until the stack is empty. We do not find any such.

Hence, DFS Visit: S, A, D, B, C

Example 2:

DFS Output: A, B, E, C, D, F, I, H, G

2) Breadth First Search (BFS):

 The BFS traversal starts at a selected vertex (source) , and explores all the neighbor vertices (level-wise traversal) directly connected to the source.  It then moves to its neighbors for starting BFS for that node, and similarly.  Hence, Breadth First Search (BFS) algorithm traverses a graph in breadth-wise.

 We Use a Queue Data Structure to remember to get the next vertex to start a search,

when a dead end occurs in any iteration.  Algorithm: The backbone of a BFS graph traversal consists of these basic steps: Step 1 – Select a source vertex and mark it as visited. Display it. Step 2 − Visit the adjacent unvisited vertex (neighbors) and mark it as visited. Display it and insert it in a queue. Step 3 − If no adjacent vertex is found, remove the first vertex from the queue. Step 4 – Select an adjacent neighbor of a visited vertex, mark it as visited and insert into queue. Step 5 − Repeat Steps 2, 3 and 4 until the queue is empty.

  • Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. Visited: S, A, B, C
  • Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A (FIFO). Visited: S, A, B, C
  • From A we have D as unvisited adjacent node. We mark it as visited and enqueue it. Visited: S, A, B, C, D

Finally, we dequeue queue until we get a vertex having an adjacent vertex. Since, there is not any unvisited vertex, the BFS Traversal is: S, A, B, C, D

Example 2:

o For an Undirected Graph:

NOTE: If A = The Adjacency Matrix of an Undirected Graph, then A[i] [j] = A[j] [i]. Hence, the matrix is symmetric. (Clue: If you fold the matrix diagonally, 1 matches with 1 and 0 matches with 0). // Edge {V1, V2} = {V2, V1} as we don’t have the Origin and Destination vertex in an undirected edge.

2. Adjacency List:

 An adjacency list is a collection of unordered lists used to represent a finite graph (finite number of vertices).  Each list describes the set of neighbors of a vertex in the graph.  Let the A[] be an array. An entry A[i] represents the list of vertices adjacent to the i th^ vertex. This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs.  Example:

Spanning Tree:

 A spanning tree is a subset of Graph G , which has all the vertices covered with minimum possible number of edges.  Hence, a spanning tree does not have cycles and it cannot be disconnected.  By this definition, we can draw a conclusion that every connected and undirected Graph G has at least one spanning tree.  A disconnected graph does not have any spanning tree , as it cannot be extended to all its vertices.  Note: Disconnected graph has spanning forest. Example:

 A graph can have many spanning trees.

Example:

o We use:Kruskal’s AlgorithmPrim’s Algorithm for finding the Minimum Spanning Tree of a graph.