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

03a Graphs

Undirected graphs are represented by nodes (V) and edges (E) between pairs of nodes. Common graph representations include adjacency matrices and adjacency lists. Basic graph concepts include paths, cycles, trees, and connectivity. Breadth-first search (BFS) explores nodes outward from a source node (s) in layers, finding all nodes at distance i from s in the ith layer. BFS can be implemented using lists or a queue. Depth-first search (DFS) recursively explores as far as possible along each branch before backtracking, and can be implemented using a stack.

Uploaded by

ap6487
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)
10 views

03a Graphs

Undirected graphs are represented by nodes (V) and edges (E) between pairs of nodes. Common graph representations include adjacency matrices and adjacency lists. Basic graph concepts include paths, cycles, trees, and connectivity. Breadth-first search (BFS) explores nodes outward from a source node (s) in layers, finding all nodes at distance i from s in the ith layer. BFS can be implemented using lists or a queue. Depth-first search (DFS) recursively explores as far as possible along each branch before backtracking, and can be implemented using a stack.

Uploaded by

ap6487
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/ 22

Basic Definitions and Applications

Undirected Graphs

Undirected graph. G = (V, E)


■V = nodes.
■E = edges between pairs of nodes.
■Captures pairwise relationship between objects.
■Graph size parameters: n = |V|, m = |E|.

V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }
n=8
m = 11

2
Some Graph Applications

Graph Nodes Edges


transportation street intersections highways
communication computers fiber optic cables
World Wide Web web pages hyperlinks
social people relationships
food web species predator-prey
software systems functions function calls
scheduling tasks precedence constraints
circuits gates wires

3
Graph Representation: Adjacency Matrix

Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge.


■Two representations of each edge.
■Space proportional to n2.
■Checking if (u, v) is an edge takes Q(1) time.
■Identifying all edges takes Q(n2) time.

1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0

4
Graph Representation: Adjacency List

Adjacency list. Node indexed array of lists.


■Two representations of each edge.
degree = number of neighbors of u
■Space proportional to m + n.
■Checking if (u, v) is an edge takes O(deg(u)) time.
■Identifying all edges takes Q(m + n) time.

1 2 3

2 1 3 4 5

3 1 2 5 7 8

4 2 5

5 2 3 4 6

6 5

7 3 8

8 3 7

5
Paths and Connectivity

Def. A path in an undirected graph G = (V, E) is a sequence P of nodes


v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is
joined by an edge in E.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and


v, there is a path between u and v.

6
Cycles

Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the
first k-1 nodes are all distinct.

cycle C = 1-2-4-5-3-1

7
Trees

Def. An undirected graph is a tree if it is connected and does not


contain a cycle.

Theorem. Let G be an undirected graph on n nodes. Any two of the


following statements imply the third.
■ G is connected.
■ G does not contain a cycle.
■ G has n-1 edges.

8
Every connected set has at least n-1 edges

• Start with n vertices and no edges.


• Add edges one after the other until the graph becomes connected.
• Each time, the number of connected components is reduced by at
most one.
• So to end up with a connected graph, a graph with one component,
you need at least n-1 edge insertions.

Every tree has n-1 edges

• Being connected, has at least n-1 edges.


• Since it contains no cycle, it has at most n-1 edges.
• Indeed, assume towards a contradiction, that it has n or more edges.
• Consider an edge and delete it. At least one new component is
created (why?).
• Therefore no more than n-1 edges can be deleted, contradicting the
assumption that at least n edges exist.

9
Rooted Trees

Rooted tree. Given a tree T, choose a root node r and orient each edge
away from r.

Importance. Models hierarchical structure.

root r

parent of v

child of v

a tree the same tree, rooted at 1

10
3.2 Graph Traversal
Connectivity

s-t connectivity problem. Given two node s and t, is there a path


between s and t?

s-t shortest path problem. Given two node s and t, what is the length
of the shortest path between s and t?

Applications.
■Maze traversal.
■Erdős number.
■Fewest number of hops in a communication network.

12
Breadth First Search

BFS intuition. Explore outward from s in all possible directions, adding


nodes one "layer" at a time.

s L1 L2 L n-1
BFS algorithm.
■L0 = { s }.
■L1 = all neighbors of L0.
■L2 = all nodes that do not belong to L0 or L1, and that have an edge
to a node in L1.
■Li+1 = all nodes that do not belong to an earlier layer, and that have
an edge to a node in Li.

Theorem. For each i, Li consists of all nodes at distance exactly i


from s. There is a path from s to t iff t appears in some layer.

13
Breadth First Search
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of
G. Then the level of x and y differ by at most 1.

The broken lines, called “cross” edges,


connect nodes none of
which is an ancestor of the other in the tree.
L0

L1

L2

L3

14
BFS: Implementation with lists

Important: The list L[i] consists of all vertices at distance i


from s.

15
Breadth First Search: Analysis

Theorem. The above implementation of BFS runs in O(m + n) time if


the graph is given by its adjacency representation.

Pf.
■ Easy to prove O(n2) running time:
– at most n lists L[i]
– each node occurs on at most one list; for loop runs £ n times
– when we consider node u, there are £ n incident edges (u, v),
and we spend O(1) processing each edge

■ Actually runs in O(m + n) time:


– when we consider node u, there are deg(u) incident edges (u, v)
– total time processing edges is SuÎV deg(u) = 2m ▪

each edge (u, v) is counted exactly twice


in sum: once in deg(u) and once in deg(v)

16
BFS: Implementation with a queue

To implement the previous algorithm with a queue,

• use a single list implemented as a queue.


• Each time a new vertex is discovered is added to the end of the
queue.
• Always process the edges incident onto the vertex that is first in
the queue,
• until all the adjacency list of this vertex is exhausted.
• Then take this vertex out of the queue.

17
BFS Implementation with a queue

Source: Dasgupta, Papadimitriou and Vazirani, Algorithms

18
Depth First Search: recursive implementation

R is the connected component that contains u

Broken lines,
called “back edges”
connect nodes
one of which is
an ancestor of the
other. No cross edges.
19
DFS: Implementation with a stack

Initially all vertices are assumed to be unexplored. You can avoid


adding to the stack already explored vertices.

We add a vertex to the DFS tree when we delete it


out of the stack and brand it “explored’.

20
Connected Component

Connected component. Find all nodes reachable from s.

Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.

21
Connected Component

Connected component. Find all nodes reachable from s.

R
s

u v

it's safe to add v

Theorem. Upon termination, R is the connected component containing s.


■BFS = explore in order of distance from s.
■DFS = explore in a different way.

22

You might also like