0% found this document useful (0 votes)
6 views

CP League Graphs and Trees

The document provides an overview of graph theory, defining key concepts such as directed and undirected graphs, adjacency matrices, and adjacency lists. It also explains graph traversal methods, specifically Depth First Search (DFS) and Breadth First Search (BFS), including their implementations and time complexities. Additionally, it includes practice problems for further learning.

Uploaded by

n.tonmoy.khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CP League Graphs and Trees

The document provides an overview of graph theory, defining key concepts such as directed and undirected graphs, adjacency matrices, and adjacency lists. It also explains graph traversal methods, specifically Depth First Search (DFS) and Breadth First Search (BFS), including their implementations and time complexities. Additionally, it includes practice problems for further learning.

Uploaded by

n.tonmoy.khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CP League Series

Session 4

Algorithms SIG, Web Club NITK


2022
What is a graph?

Formally, a graph G = (V, E); where V is the vertex set, aka a collection of points and E is
the edge set, or the set of lines joining any two given vertices.

v2
e1
v4
v1 e2

e3 e4
v3
Directed and Undirected Graphs

Directed graph:
The edges in such graphs represent a specific direction.
These graphs have ordered pairs of vertices.

Undirected graph:
The edges in undirected graphs do not have any
specific direction, and can be represented as an
unordered pair of vertices.
Adjacency Matrix

An adjacency matrix is a way of representing a given graph as a matrix of 0s and 1s.

If the graph contains n vertices, the matrix adj[n][n] is defined such that
Adj[i][j] = 1 if there exists an edge between vertex i and vertex j;
Adj[i][j] = 0 if there does not exist an edge between vertices i and j.
Adjacency List

An adjacency list represents a graph as an array of linked lists. The index of the array represents
the vertex, and the nodes of the linked list represent the vertices with which it has an edge.
Some Important Terms

Path: A sequence of vertices and edges of the graph obtained during traversal, such that no
vertex or edge is repeated.

Cycle: Obtained when the graph is traversed such that no vertex or edge is repeated, but the
starting and ending vertices are same.

e1 v2
v4
v1
e2 e4

e3
v3
Connectedness in a Graph

A graph G is said to be connected if there is a path between every pair of vertices.

Connected Graph: Disconnected Graph:

v2
v1 v1 v2

v3

v4 v4 v3
Graph Traversals

Problem
Given a starting vertex, visit all vertices that are reachable from it
Depth First Search (DFS)

Idea

1. Start from a node, say u


2. If there is a node v, such that u-v is an edge and v is not yet
visited, perform DFS(v).
Depth First Search (DFS)

Implementation
Depth First Search (DFS)

Example

6 3 4

2 5
Depth First Search (DFS)

Starting vertex = 1

1
Depth First Search (DFS)

Visited = [1]

6
Depth First Search (DFS)

Visited = [1, 6]

2
Depth First Search (DFS)

Visited = [1, 6, 2]

2
Depth First Search (DFS)

Visited = [1, 6, 2]

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5]

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5]

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5]

6 3

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5, 3]

6 3

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5, 3]

6 3 4

2 5
Depth First Search (DFS)

Visited = [1, 6, 2, 5, 3, 4]

6 3 4

2 5
Depth First Search (DFS)

n - number of vertices in the graph


m - number of edges in the graph

Time complexity : O(n +m)

Space complexity : O(n +m)


Breadth First Search (BFS)
Breadth First Search (BFS)
Idea

1. Discover vertices layer by layer, that is, at increasing distances from the starting
vertex s
2. Visit all vertices at a distance k from s before visiting vertices at a distance k + 1.
Breadth First Search (BFS)
Implementation
Breadth First Search (BFS)

Example

1 2 3

4 5 6
Breadth First Search (BFS)

Starting vertex = 1, queue = [1]

1 2 3

4 5 6
Breadth First Search (BFS)

Queue = [2, 4]

1 2 3

4 5 6
Breadth First Search (BFS)

Queue = [3, 5]

1 2 3

4 5 6
Breadth First Search (BFS)

Queue = [6]

1 2 3

4 5 6
Breadth First Search (BFS)

1 2 3

4 5 6
Breadth First Search (BFS)

n - number of vertices in the graph


m - number of edges in the graph

Time complexity : O(n +m)

Space complexity : O(n +m)


Breadth First Search (BFS)

BFS in CP problems

1. Mostly used to find the shortest path between two vertices in a graph.
2. Find the diameter of a tree.
Practice problems - 1

// Link to problems that will be solved (suggestions)

1. https://codeforces.com/problemset/problem/277/A
Practice problems - 2

// Link to problems that juniors can practice from

You might also like