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

Graphs

A graph is a data structure consisting of nodes (vertices) and edges that describe relationships between the nodes. Graphs can be directed or undirected, with various terminologies such as paths, cycles, and connected components. They can be represented using adjacency matrices or adjacency lists, and are utilized in applications like the World Wide Web and communication networks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Graphs

A graph is a data structure consisting of nodes (vertices) and edges that describe relationships between the nodes. Graphs can be directed or undirected, with various terminologies such as paths, cycles, and connected components. They can be represented using adjacency matrices or adjacency lists, and are utilized in applications like the World Wide Web and communication networks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Graphs

What is a graph?
◼ A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
◼ The set of edges describes relationships among
the vertices
Formal definition of graphs

◼ A graph G is defined as follows:


G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a finite, possible empty set of edges
(pair of
vertices)
Directed vs. undirected graphs

◼ When the edges in a graph have no


direction, the graph is called undirected

● An undirected graph is
one in which the pair of
vertices in a edge is
unordered, (v0, v1) =
(v1,v0)
Directed vs. undirected graphs
(cont.)
◼ When the edges in a graph have a direction,
the graph is called directed (or digraph)

● A directed graph is one


inWarning:
which each edgeisis a
if the graph
directed, the
directed pairorder
of of the
vertices in each edge is
vertices, <v0, v!!1> !=
important
<v1,v0>
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)
Trees vs graphs

◼ Trees are special cases of graphs!!


Graph terminology

◼ Adjacent nodes: two nodes are adjacent if they are


connected by an edge
◼ A subgraph of G is a graph G’ such that V(G’) is a
subset of V(G) and E(G’) is a subset of E(G)
◼ Path: a sequence of vertices that connect two nodes in
a graph
◼ The length of a path is the number of edges on it
◼ Complete graph: a graph in which every vertex is
directly connected to every other vertex
An Example

• Complete Graph
0 • V(G1)={0,1,2,3}
• E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)
1 2 }

3 0 0
G 1 2 2 1
1
3 3

subgraphs of G1
Graph terminology (cont.)

◼ What is the number of edges in a complete directed


graph with N vertices?
N * (N-1)
Graph terminology (cont.)
◼ What is the number of edges in a complete undirected graph
with N vertices?
N * (N-1) / 2

A complete graph is a graph


that has the
maximum number of edges
Graph terminology (cont.)
◼ Weighted graph: a graph in which each edge
carries a value
Graph terminology (cont.)
● A simple path is a path in which all vertices, except
possibly the first and the last, are distinct
● A cycle is a simple path in which the first and the last
vertices are the same
● In an undirected graph G, two vertices, v0 and v1, are
connected if there is a path in G from v0 to v1
● An undirected graph is connected if, for every pair of
distinct vertices vi, vj, there is a path from vi to vj
Graph terminology (cont.)

Connected Graphs

0 0

1 2 1 2
3
3 4 5 6
G
1 G
2
tree (acyclic graph)
Graph terminology (cont.)
● A connected component of an undirected graph
is a maximal connected subgraph.
H H
0 4
1 2

2 1 5

3 6

7
G4 (not
connected)
● A tree is a graph that is connected and acyclic.
Graph terminology (cont.)

● A directed graph is strongly connected if there is a


directed path from vi to vj and also from vj to vi.

● A strongly connected component is a maximal subgraph


that is strongly connected.
Graph terminology (cont.)

strongly connected component


not strongly connected (maximal strongly connected subgraph)

0
0 2

1
2
G
3
Graph terminology (cont.)
● The degree of a vertex is the number of edges
incident to that vertex
● For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
Graph terminology (cont.)
Degree
undirected graph
3 0
0 2
G 1 2
G 3 1 23 2 3 3
1
33 3 4 5 6
1 1 1 1
in:1, out:
0
directed graph 1
in: 1, out: in-degree
1 2 out-degree
in: 1, out:
2 0
G
Graph Representation

◼ For graphs to be computationally useful, they have to


be conveniently represented in programs

◼ There are two computer representations of graphs:


▪ Adjacency matrix representation
▪ Adjacency lists representation
Adjacency Matrix Representation

◼ In this representation, each graph of n nodes is


represented by an n x n matrix A, that is, a
two-dimensional array A

◼ The nodes are (re)-labeled 1,2,…,n

◼ A[i][j] = 1 if (i,j) is an edge


◼ A[i][j] = 0 if (i,j) is not an edge
Example of Adjacency Matrix

1
0
0 1 0 1 0
A= 0 0 1 0 0
2
0 0 1 0 0
1 0 0 0 0 4
0 0 0 4 0 3
Another Example of Adj. Matrix

◼ Re-label the nodes with numerical labels


Mary 0 Helen 1
0 0 0 0 0 0
A= 0 0 0 0 0 0 Joe 3
1 1 0 0 1 1 John 2
1 1 0 0 1 1
1 1 0 0 0 0
1 1 0 0 0 0 Tom 4 Paul 5
Pros and Cons of Adjacency
Matrices
◼ Pros:
▪ Simple to implement
▪ Easy and fast to tell if a pair (i,j) is an edge: simply
check if A[i][j] is 1 or 0

◼ Cons:
▪ No matter how few edges the graph has, the matrix
takes O(n2) in memory
Adjacency Lists Representation

◼ A graph of n nodes is represented by a


one-dimensional array L of linked lists, where

▪ L[i] is the linked list containing all the nodes


adjacent from node i.
▪ The nodes in the list L[i] are in no particular order
Example of Linked Representation
Mary 0 Helen 1
L[0]: empty
L[1]: empty Joe
L[2]: 0, 1, 4, 5 John 2 3

L[3]: 0, 1, 4, 5 Tom 4 Paul 5


L[4]: 0, 1
L[5]: 0, 1
Pros and Cons of Adjacency Lists

◼ Pros:
▪ Saves on space (memory): the representation takes as many
memory words as there are nodes and edge.

◼ Cons:
▪ It can take up to O(n) time to determine if a pair of nodes
(i,j) is an edge: one would have to search the linked list L[i],
which takes time proportional to the length of L[i].
Graph Implementation
◼ Array-based implementation
▪ A 1D array is used to represent the vertices
▪ A 2D array (adjacency matrix) is used to
represent the edges
Array-based implementation
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
7
2

G
2
G1

symmetri
undirected: c
n2/2
directed: n2
G
Graph implementation (cont.)
◼ Linked-list implementation
▪ A 1D array is used to represent the vertices
▪ A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Adjacency matrix vs. adjacency list
representation
0

1 2
3
Adjacency Matrix G Adjacency List
1

0 1 2 3
1 0 2 3
2 0 1 3
3 0 1 2
Adjacency matrix vs. adjacency list
representation
◼ Adjacency matrix
▪ Good for dense graphs --|E|~O(|V|2)
▪ Memory requirements: O(|V| + |E| ) = O(|V|2 )
▪ Connectivity between two vertices can be tested quickly

◼ Adjacency list
▪ Good for sparse graphs -- |E|~O(|V|)
▪ Memory requirements: O(|V| + |E|)=O(|V|)
▪ Vertices adjacent to another vertex can be found quickly
Graph searching
◼ Problem: find a path between two nodes of the
graph (e.g., Austin and Washington)

◼ Methods: Depth-First-Search (DFS)


or
Breadth-First-Search (BFS)
Graph Traversal (Contd.)

◼ In both DFS and BFS, the nodes of the undirected


graph are visited in a systematic manner so that every
node is visited exactly once.

◼ Both BFS and DFS give rise to a tree:


▪ When a node x is visited, it is labeled as visited, and it is
added to the tree
▪ If the traversal got to node x from node y, y is viewed as the
parent of x, and x a child of y
Depth-First Search

◼ DFS follows the following rules:


1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
Illustration of DFS
0 1
0
2
1 4
9
4
5 7
2
9 10 8
5
11 6
7 11
6 Graph G
8 10

DFS Tree
Implementation of DFS

◼ Which data structure to use ?


◼ Observations:
▪ the last node visited is the first node from which to
proceed.
▪ Also, the backtracking proceeds on the basis of "last
visited, first to backtrack too".

This suggests that a stack is the proper data structure to remember


the current node and how to backtrack.
DFS (Pseudo Code)

DFS(input: Graph G)
1. Stack S;
2. while (G has an unvisited node x)
3. visit(x);
4. push(x,S);
5. while (S is not empty)
6. t := S(top)
7. if (t has an unvisited neighbor y)
visit(y);
8. push(y,S);
9. else
10. pop(S);
11.
Depth-First-Search (DFS)

◼ What is the idea behind DFS?


▪ Travel as far as you can down a path
▪ Back up as little as possible when you reach a
"dead end" (i.e., next vertex has been "marked" or
there is no next vertex)

◼ DFS can be implemented efficiently using a


stack
start end

(initialization)
Breadth-First-Searching (BFS)
◼ What is the idea behind BFS?
▪ Look at all possible paths at the same depth
before you go at a deeper level
▪ Back up as far as possible when you reach a
"dead end" (i.e., next vertex has been "marked"
or there is no next vertex)
Illustration of BFS

0 0 1

2 4 2
1
9 4
5 9 10 5 7

10 8
11 11 6
6 7 8
Graph G
BFS Tree
BFS pseudocode
function bfs(v):
print ‘v’
enqueue{v}. a b c
mark v as visited.
d e f
while queue is not empty:
g h
u := dequeue().
for each unvisited neighbor w of v:
enqueue(w).
mark w as visited.
▪ Trace bfs(a, f) in the above graph.

▪ BFS always returns the shortest path (the one with the fewest
edges) between the start and the end vertices.
Runtime of DFS and BFS
◼ What is the expected runtime of DFS and BFS, in terms of
the number of vertices V and the number of edges E ?

◼ Answer: O(|V| + |E|)


▪ where |V| = number of vertices, |E| = number of edges
▪ Must potentially visit every node and/or examine every edge once.

◼ What is the space complexity of each algorithm?


▪ (How much memory does each algorithm require?)
start end

(initialization)
next:
Applications of Graphs
◼ World Wide Web, web pages are considered to be the vertices.
There is an edge from a page u to other page v if there is a link
of page v on page u. This is an example of Directed graph.
◼ In Computer science graphs are used to represent the flow of
computation.
◼ Communication Networks, vertices in graph represent
terminals, processors and edges represent transmission channels
like wires, fibers etc. through which the data flows.
◼ In Operating System, we come across the Resource Allocation
Graph where each process and resources are considered to be
vertices. Edges are drawn from resources to the allocated
process, or from requesting process to the requested resource.

You might also like