0% found this document useful (0 votes)
21 views40 pages

CS 412 - T2311 - CH 22 - Part1

Uploaded by

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

CS 412 - T2311 - CH 22 - Part1

Uploaded by

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

Imam Abdulrahman Bin Faisal University

College of Computer Science and Information Technology

Department of Computer Science

CS 412 Algorithm Analysis and Design


Term 1 2024-2025

Chapter 22 – Part 1
Instructor:
Dr. Salma Alzahrani

Elementary Graph Algorithms


Graphs
Definition = A graph is a structure that consists a set of nodes
(vertices) with edges (links) between pairs of vertices.

V = {0,1,2,3,4}
E = {(0,1), (1,2), (2,3), (3,4), (0,4), (1,4),(1,3)}

 G = (V, E) - A graph G is a pair (V,E) where


 V = set of vertices V = n
 E = set of edges (pairs of vertices) E = m
 Binary relation on V (is a set of pairs of vertices)
 Subset of V x V ={(u,v): u V, v V}

2
Graph
 Example 1:
V = {A, B, C, D}
E = {(A,B), (B,C), (B,D),
(C,D)}

 Example 2:

3
Applications
 Applications that involve not only a set of items, but
also the connections between them

Maps Schedules Computer networks

Hypertext Circuits
4
Directed Vs. undirected graph
 Directed edge
• ordered pairs of vertices (u,v)
 Undirected edge
• unordered pairs of vertices (u,v)
 A graph with directed edges is called
a directed graph or digraph
 If graph is directed, a vertex
has a separate in/out degree.
 A digraph can be weighted or
unweighted.

 A graph with undirected edges is an


undirected graph or simply a graph
 Undirected graph can be
weighted or unweighted.

5
Directed Vs. undirected graph
Directed vs Undirected graphs

Directed graphs degree

6
Directed Vs. undirected graph
Given a directed graph, count the in and out degree of each
vertex of the graph.

Vertex In Out
0 1 2
1 2 1
2 2 3
3 2 2
4 2 2
5 2 2
6 2 1

7
Weighted Graphs
 The edges in a graph may have values associated with them
known as their weights
 A graph with weighted edges is known as a weighted graph

 Examples:

8
Terminology
 End vertices (or endpoints) of an
edge
 U and V are the endpoints of a
 Edges incident on a vertex
 a, d, and b are incident on V V
a b
 Adjacent vertices h j
 U and V are adjacent U d X Z
 Degree of a vertex c e i
 X has degree 5 W g
 Parallel edges

f
h and i are parallel edges Y
 Self-loop
 an edge of a graph which starts
and ends at the same vertex.
 j is a self-loop 9
Terminology (cont.)
 A path is a sequence of vertices in which each successive
vertex is adjacent to its predecessor
 Path from v to w: A sequence of vertices <v0, v1, …, vk>
such that v0=v and vk=w , for i=1,…k, vi-1 is adjacent to
vi
 Equivalently, p is a sequence of edges e1, …, ek where for
i = 2,…k each consecutive pair of edges ei-1, ei share a
vertex
 In a simple path, the vertices and edges are distinct (is aVpathb
a
with no repeated vertices.) P1
 Length of a path d
U X Z
 Number of edges in the path
P2 h
c e
 Examples W g
 P1=(V,X,Z) is a simple path
f
 P2=(U,W,X,Y,W,V) is a path that is not simple
Y 10
Terminology (cont.) 1 2
 A cycle is a path in which the first and final
vertices are the same
 A path <v0, v1, …, vk> forms a cycle if v0=vk 3 4
and k≥2 A simple cycle from v1 to
 v1
Simple cycle
<v1, v2, v3,v1>
 cycle such that all its vertices and edges are V
a b
distinct except the first and final vertices are
the same U d X Z
 C2 h
Examples e
c C1
 C1=(V,X,Y,W,U,V) is a simple cycle. W g
 C2=(U,W,X,Y,W,V,U) is a cycle that is not f
simple
Y
 Acyclic graph (Non-cyclic graph)
 A graph without any cycles
11
Terminology (cont’d)
 Complete graph
 A graph with an edge between each pair of vertices

Example
In the following graphs, each vertex in the graph is connected with all the
remaining vertices in the graph except by itself.

12
Terminology (cont’d)
 Subgraph S of a graph G is a graph such that
 A graph S = (V’, E’) such that V’V and E’E
 The vertices of S are a subset of the vertices of G Subgraph
 The edges of S are a subset of the edges of G
 A subgraph S of a graph G is a graph whose set of vertices and set of edges are
all subsets of G. (Since every set is a subset of itself, every graph is a subgraph of
itself.)
 A spanning subgraph of G is a subgraph that contains all the vertices of
G
 If a subgraph S is drawn by:
 removing only a few (or all) edges.
Spanning
 but retaining all the vertices (points) of a graph G, subgraph
 the subgraph S is called as a Spanning Subgraph.

13
Terminology (cont’d)
Example:
 We have obtained H by retaining all vertices and deleting just one edge
(3). Hence H is a subgraph which is also a spanning subgraph.

 In the case of H’ we have drawn it by deleting a vertex ‘e’ and the edges
(4,5) which are incident to it. We have not retained all the vertices of G.
Hence H’ is not a spanning subgraph. It’s a just a subgraph .
14
Connectivity
Terminology (cont’d)
 A graph is connected if there is a
path between every pair of vertices.

Connected graph

 A connected component of a graph


G is a maximal connected subgraph
of G

Non connected graph with


two connected
components
15
Terminology (cont’d) - Trees and
Forests
 A tree is an undirected graph T such
that
T is connected
 T has no cycles (acyclic)

Tree

 A forest is a collection of an acyclic


undirected graph which is not connected.
 The connected components of a forest are
trees.
 In other words, Forest is a collection of
disjoint trees Forest

16
DAG
 A directed acyclic graph (DAG) is a digraph that has no directed cycles.
 a graph that is directed and without cycles connecting the other
edges.
 This means that it is impossible to traverse the entire graph starting
at one edge. The edges of the directed graph only go one way.

D E

A DAG G

17
Spanning Trees and Forests
 A spanning tree of a connected graph is a
spanning subgraph that is a tree
 A spanning tree is not unique unless the
graph is a tree

Graph

Spanning tree

18
Linked Lists, Trees, Graphs
 A binary tree is a graph with some restrictions:
 The tree is an unweighted, directed, acyclic graph (DAG).
 Each node's in-degree is at most 1, and out-degree is at most 2.
 There is exactly one path from the root to every node.
F

B K

 A linked list is also a graph: A E H


 Unweighted DAG.
 In/out degree is at most 1 for all nodes. G J

A B C D

19
Dense Vs. Sparse Graphs
 If a graph has |V| vertices, how many edges can it have?
 The first vertex can have an edge to every other vertex |V-1| edges
 ... and so on for each of the |V| vertices; and all these edges are
distinct

 So, For directed graph: the maximum total number of edges Nodes = n
possible is |E| = |V|x|V-1| = (|V|2 - |V| ) Edges = n(n-1)/2
 For undirected graph: |E| = |V|x|V-1| / 2 = (|V|2 - |V| )/2
 Dense Graph:
 A graph where |E| is close to |V|2 edges is considered dense
 A dense graph is a graph G=(V,E) in which |E|=ϴ(|V|2).

 Sparse Graph:
 A graph where |E| is close to |V| is considered sparse.
 A sparse graph is a graph G=(V,E) in which |E|= ϴ(|V|).

20
Representation of Graphs
Two standard ways:
1. Adjacency List
 Preferred for sparse graphs (|E| is much less than |V|2)
 PreferredWe need to quickly determine the nodes adjacent
to a given node.
 Unless otherwise specified we will assume this representation
2. Adjacency Matrix
 Preferred for dense graphs (|E| is close to |V|2).
 Preferred
when we need to be able to tell quickly if there is
an edge connecting two given vertices (permit faster edge
lookup)

Either way applies to both directed and undirected graphs.


21
Adjacency List
 Adjacency list representation of G = (V, E)
 An array Adj of V lists, one for each vertex in V
 For each vertex u in V:
 The adjacency list Adj[u] contains all the vertices v that are
adjacent to u (i.e., there is an edge from u to v: (u,v) ϵ E)
 Can be used for both directed and undirected graphs.

1 2 5 /
1 2 2 1 5 3 4 /

3 3 2 4
4 2 5 3 /
5 4
5 4 1 2
Undirected graph 22
Properties of Adjacency-List Representation
 Space required for the adjacency-list representation is: Θ(|V|+|E|) (we

will use V for |V| and E for |E|) thus Θ(V+E) For both directed and
1 2
undirected graphs

 Sum of “lengths” of all adjacency lists


3 4

 We have |V| lists, and although each list could have as many as |V|-1Directed graph

vertices, in total the adjacency lists for


E  1 2
 Directed graph: contains elements. 3

 edge (u, v) appears only once (i.e., in the list of u) 5 4

2 E 
 Undirected graph: contains elements Undirected graph

 edge (u, v) appears twice (i.e., in the lists of both u and v) 23


Properties of Adjacency-List Representation
 Memory required
 (V + E)
1 2
 Preferred when
3
 The graph is sparse: E  << V 2
5 4
 We need to quickly determine the nodes adjacent to a
given node.
Undirected graph
 Disadvantage
 No quick way to determine whether there is an edge 1 2
between node u and v (to determine whether a given edge
(u,v) is present in the graph need to search for v in the
adjacency list Adj[u].) 3 4
 Time to determine if (u, v)  E: O(degree(u)) Directed graph
 we go to u’s adjacency list in constant time and then
 look for v in u’s adjacency list, in O(degree(u)). 24
Adjacency List --Example

1 2 5
1
2 1 5 4
5 2 3
4
4
2 3 5
4 3 5
4 1 2

25
Adjacency List -- Example

A B A B E
B C D
C
C
D
E D c E
E

26
Adjacency Matrix
 Adjacency matrix representation of G = (V, E)
 Assume vertices are numbered 1, 2, … V 
 The representation consists of a matrix A V x V  :
 aij = 1 if (i, j)  E
• For undirected graphs,
0 otherwise
1 2 3 4 5 matrix A is symmetric:
1 2 1 0 1 0 0 1 aij = aji
3 2 1 0 1 1 1 A = AT
5 4 3 0 1 0 1 0 • The row i, column j entry is 1
4 0 1 1 0 1 if and only if the row j,
Undirected graph column i entry is 1.
5 1 1 0 1 0
• For a directed graph, the
adjacency matrix need not
be symmetric. 27
Properties of Adjacency Matrix Representation
 Memory required
 (V2), independent on the number of edges in G
1 2
 Preferred when
 The graph is dense: E is close to V 2 3

 We need to quickly determine if there is 5 4


an edge between two vertices
Undirected graph
 Time to determine if (u, v)  E:
 (1)
1 2
 Disadvantage
 No quick way to determine the vertices
3 4
adjacent to another vertex
 Directed graph
Time to list all vertices adjacent to u:
 28 )
(V) (You need to check the row for v, (which has |V| columns) to find which ones are adjacent
Adjacency Matrix -- Example

1 2 3 4 5
1 0 1 0 0 1
1
2 1 0 0 1 1

5 2 3 0 0 0 1 0
4 0 1 1 0 1
4 3 5 1 1 0 1 0

29
Adjacency Matrix -- Example

A B C D E
A B
A 0 1 0 0 1
B 0 0 1 1 0
C
C 0 0 0 0 0

E D D 0 0 1 0 1
E 0 0 0 0 0

30
Adjacency List and Matrix --
Example

31
Adjacency List and Matrix --
Example

32
Weighted Graphs
 Graphs for which each edge has an associated weight
w(u, v)

w: E  R, weight function
 Storing the weights of a graph
 Adjacency list:
 Store w(u,v) along with vertex v in u’s adjacency list
 Each item in each adjacency list is either a two-item
array or an object, giving the vertex number and the
edge weight.

 Adjacency matrix:
 Store w(u, v) at location (u, v) in the matrix
33
Weighted Graphs

34
Graph Traversals
 For solving most problems on graphs
 Need to systematically visit all the vertices and edges of a
graph

 Search vs Traversal
 Search: Look for a given node.
 stop when node found, even if not all nodes were visited
 Traversal: Always visit all nodes

 Two major traversals


1) Breadth-First Search (BFS).
2) Depth-First Search(DFS)

35
Problem 1
 (Exercise 22.1-1, page 530) Given an adjacency-
list representation, how long does it take to
compute the out-degree of every vertex?
 For each vertex u, search Adj[u] = Θ(|Adj(v)|).
 The sum of the lengths of all the adjacency lists in Adj is |
E|.
 Thus the time to compute the out-degree of one vertex is
Θ(|Adj(v)|) and for all vertices is Θ(V + E)

A B A B E
B C D
C
C
D
E D c E
E

36
Problem 1
 How long does it take to compute the in-degree of every vertex?
 The in-degree of a vertex u is equal to the number of times it appears in all
the lists in Adj. If we search all the lists for each vertex, The naïve computation of in-
degree for every vertex is Theta(V^2+V*E).
 Alternatively, we can allocate an array T of size |V | and initialize its entries to
zero. Then we only need to scan the lists in Adj once, incrementing T[u] when
we see u in the lists. The values in T will be the in-degrees of every vertex.
This can be done in Θ(V + E) time with Θ(V ) additional storage.

A B A B E
B C D
C
C
D
E D c E
E

37
Problem 1
 How long does it take to compute the in-degree of every vertex?
 The in-degree of a vertex u is equal to the number of times it appears in all
the lists in Adj. If we search all the lists for each vertex, The naïve computation of in-
degree for every vertex is Theta(V^2+V*E).

 NOTE A FUNNY THING HERE: The naïve version may look as if it is Theta (V^2),
but this formula does not apply for very sparse graphs, for example
E=Theta(1), or E=Theta(log V) in these cases the formula Theta (V^2) is
incorrect.
A B A B E
B C D
C
C
D
E D c E
E
38
Problem 1
 Describe how to compute the in-degree and out-degree of the
vertices of a graph given its adjacency-matrix representation.
 The adjacency-matrix A of any graph has Θ(V2) entries, regardless of the
number of edges in the graph. For a directed graph, computing the out-
degree of a vertex u is equivalent to scanning the row corresponding to u in A
and summing the ones, so that computing the out-degree of every vertex is
equivalent to scanning all entries of A. Thus the time required is Θ(V ) for one
vertex, and Θ(V2 ) for all vertices.
 Similarly, computing the in-degree of a vertex u is equivalent to scanning the
column corresponding to u in A and summing the ones, thus the time
required is also Θ(V ) for one vertex, andAΘ(VB2 ) C
for D
all vertices.
E
A B
A 0 1 0 0 1
B 0 0 1 1 0
C
C 0 0 0 0 0

E D D 0 0 1 0 1
E 0 0 0 0 0
39
References:
• T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, “Introduction to
Algorithms”, The MIT Press, 2009, ISBN-10: 0262033844 | ISBN-13: 978-
0262033848, 3rd Edition  chapter 22.
• https://www.cse.unr.edu/~bebis/CS477/
• https://www.geeksforgeeks.org/finding-in-and-out-degrees-of-all-vertices-in
-a-graph/
• https://www.tutorialspoint.com/graph_theory/types_of_graphs.htm
• https://www.quora.com/What-is-a-spanning-subgraph
• http://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec11/lec11-6.html
• https://www.khanacademy.org/computing/computer-science/algorithms/gra
ph-representation/a/representing-graphs

40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy