Unit 3 Graph.pptx
Unit 3 Graph.pptx
• A graph is called an infinite graph if it has an infinite number of vertices and an infinite number of edges
Trivial – Null Graph
• A graph G= (V, E) is trivial if it contains only a single vertex and no edges.
• It's a reworked version of a trivial graph. If several vertices but no edges connect them, a graph G= (V, E) is a
null graph.
Multi – Pseudo Graph
• If there are parallel edges between a pair of vertices in a graph G= (V, E), the graph is referred to as a
multigraph.
• There are no self-loops in a Multigraph.
• If a graph G= (V, E) is a simple graph with the same degree at each vertex, it is a regular graph.
Complete or Full Graph
• If a graph G= (V, E) is also a simple graph, it is complete.
• Using the edges, with n number of vertices must be connected.
• It's also known as a full graph because each vertex's degree must be n-1 where n is number of vertices.
• A complete graph has n(n–1)/2 edges, where n is the number of nodes in G.
Weighted – Unweighted Graph
• A graph G= (V, E) is called a labelled or weighted graph because each edge has a value or weight
representing the cost of traversing that edge.
Directed – Undirected Graph
• A directed graph also referred to as a digraph, is a set of nodes connected by edges, each with a direction.
• The order of the two connected vertices is irrelevant and has no direction.
Sparse – Dense Graph
• A sparse graph with relatively few edges compared to the number of vertices.
• A dense graph with many edges compared to the number of vertices.
• When there is no edge linking the vertices, you refer to the null graph as a disconnected graph.
Cyclic – Acyclic Graph
• If a graph contains at least one graph cycle, it is considered to be cyclic.
• When there are no cycles in a graph, it is called an acyclic graph.
Vertex (Node) A fundamental part of a graph representing an object or point. In a social network graph, each person is a vertex.
Edge (Link) A connection between two vertices in a graph. In a social network graph, a friendship between two people is an edge.
Degree The number of edges connected to a vertex. In a social network, if a person has three friends, their degree is 3.
In-Degree The number of incoming edges to a vertex in a directed graph. In a Twitter network, the number of followers a person has.
Out-Degree The number of outgoing edges from a vertex in a directed graph. In a Twitter network, the number of people a person follows.
In a city map graph, a path represents the route from one city to another
Path A sequence of vertices connected by edges.
through connecting roads.
• The adjacency matrix above represents an undirected graph, so the values '1' only tells us where the edges are.
• Also, the values in the adjacency matrix is symmetrical because the edges go in both ways (undirected Graph).
Contd…
• To create a directed graph with an adjacency matrix, we must decide which vertices the edges go from and to, by
inserting the value at the correct indexes (i,j).
• In the adjacency matrix above, the value 3 on index (0,1) tells us there is an edge from vertex A to vertex B and the
weight for that edge is 3.
Adjacency List Graph Representation
• A sparse graph is a graph where each vertex only has edges to a small portion of the other vertices in the Graph.
• An Adjacency List has an array that contains all the vertices in the graph, and each vertex has a Linked List with the edges
of respective vertices.
• In the adjacency list above, the vertices A to D are placed in an Array and each vertex in the array uses array index value.
• The Linked List contains the indexes to the adjacent vertices.
Contd….
• An Adjacency List can also represent a directed and weighted Graph.
1. Push the starting node onto the stack. 1. Start at the initial node.
2. While the stack is not empty, pop a 2. Mark the current node as visited.
node from the stack. 3. For each unvisited neighbour, call the
3. If the node is unvisited, mark it as DFS function recursively.
visited. 4. Continue until all nodes connected to
4. Push all unvisited neighbours onto the starting node are visited.
the stack.
5. Repeat until the stack is empty.
DFS Recursive Approach
int adj[MAX][MAX]; // Adjacency matrix int main()
int visited[MAX]; // Visited array {
int n; // Number of vertices n = 5; // Example graph with 5 vertices
S := EmptyStack
visited[s] := true
Push(S,s)
• Space Complexity
• The space complexity of a depth-first search is O(V).
• Completeness
• This is a complete algorithm because if there exists a solution, it will be found regardless of the type of graph provided.
Applications
• Topological Sorting
• Finding Strongly Connected Components in a Graph
• Path Finding
• Detecting a graph's cycle
Breadth First Search - BFS
• BFS is a method for systematically visiting all nodes (vertices) in a graph or tree.
• It explores nodes level by level, starting from a given source node and moving outwards.
• BFS uses a queue (a FIFO - First-In, First-Out data structure) to keep track of the nodes to be explored.
• BFS is particularly useful for finding the shortest path in unweighted graphs (where all edges have the same
weight).
BFS Algorithm
1. Start: Begin at a designated starting node (or root node).
2. Enqueue: Add the starting node to the queue.
3. Iterate:
• Dequeue a node from the front of the queue.
• Mark the dequeued node as visited.
• Explore all unvisited neighbours of the dequeued node.
• Enqueue all unvisited neighbours into the queue.
4. Repeat: Continue steps 3 until the queue is empty.
BFS Implementation
int queue[MAX], front = -1, rear = -1; void BFS(int graph[MAX][MAX], int start, int int main()
n)
int visited[MAX] = {0}; {
{
int graph[MAX][MAX], n, start;
enqueue(start);
void enqueue(int v) printf("Enter the number of vertices: ");
visited[start] = 1;
{ scanf("%d", &n);
printf("BFS Traversal: ");
if (rear == MAX - 1) return; printf("Enter the adjacency matrix:\n");
while (front != -1)
if (front == -1) front = 0; for (int i = 0; i < n; i++)
{
queue[++rear] = v; for (int j = 0; j < n; j++)
int v = dequeue();
} scanf("%d", &graph[i][j]);
printf("%d ", v);
int dequeue()
for (int i = 0; i < n; i++)
{ printf("Enter the starting vertex: ");
{
if (front == -1) return -1; scanf("%d", &start);
if (graph[v][i] && !visited[i])
int v = queue[front++]; BFS(graph, start, n);
{
if (front > rear) front = rear = -1; return 0;
enqueue(i);
return v; }
visited[i] = 1;
}
} } } }
BFS Properties
• Shortest Path in Unweighted Graphs
• BFS guarantees finding the shortest path (in terms of number of edges) in an unweighted graph.
• Simple to Implement
• The algorithm is relatively straightforward to understand and implement.
• Systematic Exploration
• It explores the graph in a methodical way, ensuring all reachable nodes are visited.
• All-Pairs Algorithms:
1. Floyd-Warshall Algorithm:
• A dynamic programming algorithm that finds the shortest paths between all pairs of nodes in a
graph.
• It can handle negative edge weights but not negative cycles.
2. Johnson's Algorithm:
• An algorithm that combines Dijkstra's algorithm with the Bellman-Ford algorithm to find
shortest paths between all pairs of nodes in a graph with potentially negative edge weights.
Contd…
• The shortest path from vertex D to vertex F in the Graph
• The Bellman-Ford algorithm is best suited to find the shortest paths in a directed graph with
one or more negative edge weights from the source vertex to all other vertices.
• It works fine with negative edges as well as it is able to detect if the graph contains a
negative cycle.
• Relaxation means updating the shortest distance to a node if a shorter path is found through
another node.
• For an edge (u, v) with weight w:
• distance[v] > distance[u] + w), then update the distance[v] = distance[u] + w.
• In the bellman-ford algorithm, this process is repeated (V – 1) times for all the edges.
• This algorithm is only applicable for directed graphs.
Negative Weight Cycle
• Negative Cycle: A cycle is called a negative cycle if the sum of all its weights becomes negative.
• The following illustration is an example of a negative cycle:
1. Create a distance matrix dist[][] where dist[i][j] is the weight of the edge from i to j.
2. Initialize:
dist[i][j] = 0 if i == j
dist[i][j] = weight if edge exists
dist[i][j] = ∞ if no edge