0% found this document useful (0 votes)
22 views28 pages

Graphs

The document provides an overview of graph theory, including definitions, types of graphs, and key concepts such as trees, graph traversal algorithms (BFS and DFS), and bipartite graphs. It explains the properties of trees, the applications of BFS and DFS in connectivity and search, and how to determine strongly connected components in directed graphs. Additionally, it discusses the implications of these concepts in practical scenarios, such as social networks and message broadcasting.

Uploaded by

Krishika Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views28 pages

Graphs

The document provides an overview of graph theory, including definitions, types of graphs, and key concepts such as trees, graph traversal algorithms (BFS and DFS), and bipartite graphs. It explains the properties of trees, the applications of BFS and DFS in connectivity and search, and how to determine strongly connected components in directed graphs. Additionally, it discusses the implications of these concepts in practical scenarios, such as social networks and message broadcasting.

Uploaded by

Krishika Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Graphs

• Graphs specify pairwise relationships between objects.


• Notation G = (V, E)
○ V is a set of nodes, aka vertices.
○ E is a set of two-elements subset of V.
• Graph size parameters: n = | V |, m = | E |

• Directed Graphs
• Undirected Graphs

Terminology

• Simple: at most one edge between every pair of vertices.


• Complete: all possible edges are present (denoted: Kn)
• Degree of a vertex: number of incident edges.
• Self-loop
• Path : A path in an undirected graph G = (V, E) is a sequence of nodes
v1, v2, …, vk with the property that each consecutive pair vi–1, vi is joined
by an edge in E.
• Simple path: a path with no vertex repeated. All nodes/vertices are
distinct
• Closed path: a path with v1 = vk . This path is a cycle if it is
• simple.
• A graph is connected if there is a path between any pair of
• vertices.
• Digraph: short name for directed graph.
• (Connected) Component: a maximal connected subgraph
• Cut-edge (aka bridge): an edge whose removal increases the # of
connected components
Tree
An undirected graph is a tree if it is connected and does not contain a cycle.

The following statements are equivalent:


1 T is a tree.
2 T contains no cycles and n - 1 edges.
3 T is connected and has n - 1 edges.
4 T is connected and every edge is a cut-edge.
5 Any two nodes in T are connected by exactly 1 path.
6 T is acyclic, and adding any new edge creates exactly one cycle.

• A tree is rooted if it has a distinguished vertex called the root.


• A tree is ordered if it is a rooted tree where the children are assigned an order.
• In a binary tree, each node has at most 2 children.
• In a m-ary tree, each node has at most m children.
• A complete tree is a m-ary tree for which each node has m children and all leaves are at the same level.
Graph Applications
Graph Traversal
• Are used for
• searching the graph
• systematically traversal of graph
• define structure of graph

• Two Algorithms
• BFS - Breadth First Search
• DFS - Depth First Search
BFS
Sunday, February 26, 2023 11:05 AM
BFS Example

• Implementation is using Queues


BFS: When you see a new, unexplored node, put it on a queue. Will process nodes
in the order you first see them.

M
BFS Analysis
DFS Analysis
Applications of BFS

1. To find whether two given nodes are connected


2. To find shortest path between nodes
3. To find whether the graph is connected or not
4. To find the connected components of the graph
5. To check the bipartiteness of graph

To find shortest path between nodes


For each node, its parent and distance from source will be maintained
Bipartite graphs

An undirected graph G = (V, E) is bipartite if the nodes can be colored


blue or white such that every edge has one white and one blue end.

Testing bipartiteness

Testing Using BFS

Lemma : Let G be a connected graph, and let L0, …, Lk be the layers produced
by BFS starting at node s. Exactly one of the following holds :
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
Lemma : Let G be a connected graph, and let L0, …, Lk be the layers produced
by BFS starting at node s. Exactly one of the following holds :
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
(ii) An edge of G joins two nodes of the same layer, and G contains an odd-length cycle (and hence is not
bipartite).

Proof (i)
Suppose no edge joins two nodes in same layer.
By BFS property, each edge join two nodes in adjacent levels.
Bipartition: white = nodes on odd levels, blue = nodes on even levels.

Proof (ii)
Suppose (x, y) is an edge with x, y in same level Lj.
Let z = lca(x, y) = lowest common ancestor.
Let Li be level containing z.
Consider cycle that takes edge from x to y, then path from y to z, then path from z to x.
Its length is 1 + (j – i) + (j – i), which is odd
Applications of DFS

Connectivity and Search in Directed Graphs

• Directed reachability : Given a node s, find all nodes reachable from s.


• Find strongly connected components of the graph
• Topological Sorting

Can we use BFS for directed graphs to check reachability/connectivity?


Which one is better BFS or DFS to find the shortest path from s to t in a directed graph. Justify

Example :
Web crawler. Start from web page s. Find all web pages linked from s, either directly or
indirectly.
Strongly Connected Components of Directed Graph

Mutually reachable : Nodes u and v are mutually reachable if there is a both path from
u to v and also a path from v to u.

Strongly Connected : A graph is strongly connected if every pair of nodes is mutually


reachable.

Lemma. Let s be any node. G is strongly connected iff every node is reachable from s,
and s is reachable from every node.
Pf. ⇒ Follows from definition.
Pf. ⇐ Path from u to v: concatenate u↝s path with s↝v path.
Path from v to u: concatenate v↝s path with s↝u path. ▪

A strong component is a maximal subset of mutually reachable nodes.

Algorithm to check strong connectivity/Find strongly connected components

Using BFS
• Pick any node s.
• Run BFS from s in G.
• Run BFS from s in Greverse.
• Return true iff all nodes reached in both BFS executions.
• Correctness follows immediately from previous lemma.
• Time complexity is O(m + n) time

Using DFS
• discovery time: d[u] = the time at which u is first visited
• finishing time: f [u] = the time at which all u and all its neighbors have been visited.
• Clearly d[u] < f [u].
• Every edge (u; v) in a DAG has f [v] < f [u].
• If we list nodes from largest f [u] to smallest f [u] then every edge goes from left to right.
• Exactly a topological sort.
• So: as each node is finished, add it to the front of a linked list.
Question

Following diagram represents a social network. A line between two people means that
they are connected on a meeting app. Message broadcasting is a chain reaction in this
network. When a person broadcasts a message to all his contacts, his contacts in turn
broadcast the message to all their contacts and so on. Write an algorithm to check
whether the message broadcasted by any given person 'x' in the network reaches to
each and every other person in the network. In the following instance show the path of
transmission from Audrey to all other persons using the algorithm. Also check
whether it is possible to divide people in this social network in two groups such that
people belonging to a group are not connected on the social media.

You might also like