











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












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 :
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.
bidirectional (i.e. connected with undirected edges).
matter.
Example:
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:
vertices.
Example:
Any graph which contains some parallel edges but doesn’t contain any self-loop is called multi 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.
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
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:
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
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.
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.
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.
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 Algorithm Prim’s Algorithm for finding the Minimum Spanning Tree of a graph.