0% found this document useful (0 votes)
219 views75 pages

Algo Mod6 Graphs

This document defines key terminology related to graphs. It discusses: - Graphs are represented by (V,E) where V is the set of vertices and E is the set of edges. - Terminology includes adjacent/incident vertices/edges, paths, cycles, connected/strongly connected graphs, subgraphs, and more. - Undirected graphs represent edges as pairs {u,v} while directed graphs use ordered pairs (u,v) indicating direction of edges. - Properties like completeness, connectivity, and degrees are defined for both undirected and directed graphs.

Uploaded by

ISSAM HAMAD
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)
219 views75 pages

Algo Mod6 Graphs

This document defines key terminology related to graphs. It discusses: - Graphs are represented by (V,E) where V is the set of vertices and E is the set of edges. - Terminology includes adjacent/incident vertices/edges, paths, cycles, connected/strongly connected graphs, subgraphs, and more. - Undirected graphs represent edges as pairs {u,v} while directed graphs use ordered pairs (u,v) indicating direction of edges. - Properties like completeness, connectivity, and degrees are defined for both undirected and directed graphs.

Uploaded by

ISSAM HAMAD
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/ 75

Algorithms….

Graphs
Dr. ISSAM ALHADID
Modification date 4/3/2019
Outlines: Graphs
Applications: Communication
Network
 vertex = city, edge = communication link
Street Map
 Some streets are one way
 A bidirectional link represented by 2

directed edge : (5, 9) (9, 5)


Computer Networks
 Electronic Circuits cslab1a cslab1b
◦ Printed Circuit Board
◦ Integrated Circuits (ICs)
 Computer networks
◦ Local Area Network (LAN)
◦ Internet
◦ Web
Graph Terminology
◦ A graph G = (V, E) consists of two sets V and E. The
elements of V are called the vertices and the
elements of E the edges of G.
◦ Each edge is a pair of vertices. For instance, the sets
V = {1, 2, 3, 4, 5} and E = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
define a graph with 5 vertices and 4 edges.
◦ Graphs have natural visual representations in which
each vertex is represented by a point and each edge by
a line connecting two points.

◦ Figure 1: Graph G = (V, E) with V = {1, 2, 3, 4, 5} and


E = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
Graph Terminology
 A graph G = (V, E):
◦ V = set of vertices (nodes or points).
◦ E = set of edges (arcs or lines) = subset of V × V.
 In describing the running time of a graph
algorithm on a given graph G = (V, E), we
usually measure the size of the input in terms
of the number of vertices |V| and the number
of edges |E| of the graph.
 Thus, |E| ≤ |V|2 = O(|V|2).

 if V= 4 then |E| ≤ 16 
Graph Terminology
 A graph is an ordered pair (V, E), where V is
called the vertex set and E is called the edge
set. With each e ∈ E, we associate two
vertices which we call the ends of e.
Graph Terminology
 Undirected graph; an edge is an unordered
pair of vertices {v1, v2}
◦ edge(v1, v2) = edge(v2, v1).

 Directed graph (called digraph): an edge is an


ordered pair of vertices (v1, v2)
◦ edge(v1,v2) goes from vertex v1 to vertex v2,
notated v1 → v2.
◦ edge(v1, v2) is NOT the same as edge(v1, v2)
Graph Terminology
 A directed graph, called digraph for short, is a
ordered pair (V, E) – as usual V are vertices and E
edges.
 If f(e) = (u, v), we call u the tail of e and v the

head of e. So, if f(e) = (u, v) we can represent this


as being able to travel from u to v along edge e,
but we cannot use edge e to travel from v to u.
The edges now have a direction associated with
them.
 If I use the term “graph”, I mean an undirected

graph.
Graph Terminology
Graph Terminology
 Two representations of an undirected graph. (a) An
undirected graph G having five vertices and seven
edges. (b)The adjacency-matrix representation of G.

(a)
(b)
Graph Terminology
 Two representations of a directed graph. (a) A
directed graph G having six vertices and eight
edges. (b) The adjacency-matrix representation
of G.
Graph Terminology
 adjacency-matrix representation of a graph
G = (V, E), we assume that the vertices are
numbered 1, 2, . . . , |V| in some arbitrary manner.
Then the adjacency-matrix representation of a
graph G consists of a |V| × |V| matrix A = (aij )
such that:
Graph Terminology
 Adjacent vertices connected by an edge:
◦ Vertex v is adjacent to u if and only if (u, v) ∈E.
◦ In an undirected graph with edge (u, v), and hence
(v, u), v is adjacent to u and u is adjacent to v.

 Two vertices are called adjacent if they are


connected by an edge.
Graph Terminology
 Two edges are called incident, if they share a
vertex. Also, a vertex and an edge are called
incident, if the vertex is one of the two
vertices the edge connects.
 Incident edges on a vertex:

◦ For example, edges:1, 2, and 3 are incident on


vertex a.
 For example Adjacent vertices of 11 are 7,5 But 2,9,10 are
NOT.
 Incidence is placed between and edge and vertex, thus Incident

edge a incidents on vertex 11 from vertex 7, same way


incident edge d incidents from vertex 11 and incidents on
vertex 9.
Graph Terminology
 Incident From
 e is incident from u
 v is incident from e.

 Incident To
 e is incident to v
 u is incident to e.
Graph Terminology
 A Path in a graph from u to v is a sequence of edges
between vertices w0, w1, …, wk, such that (wi, wi+1)
∈E, where u = w0 and v = wk, for 0 ≤ i < k.
◦ (wi, wi+1) ∈E  we have edges between vertices w i and wi+1
 The length of the path is k, which is the number of
edges on the path.
Graph Terminology
 Loops: If the graph contains an edge (v, v)
from a vertex to itself, then the path (v, v ) is
sometimes referred to as a loop.

 The graphs we will be consider generally


loopless.
Graph Terminology
 Path:
◦ sequence of alternating vertices and edges (follow
one another)
◦ begins with a vertex.
◦ ends with a vertex.
◦ each edge is preceded and followed by its
endpoints (vertices).
 Simple path:
◦ path such that all its vertices and edges are
distinct.
Graph Terminology
 A simple path is a path such that all vertices
are distinct, except that the first and last
could be the same.
Graph Terminology
 Examples:
 P1 = (V, X, Z) is a simple path.
 P2 = (U, W, X, Y, W, V) is a path that is not

simple.
Graph Terminology
 Cycles:
◦ A cycle in a directed graph is a path of length at
least 2 such that the first vertex on the path is the
same as the last one; if the path is simple, then
the cycle is a simple cycle.
Graph Terminology
 Cycles:
◦ A cycle in a undirected graph is a path of length
at least 3 such that the first vertex on the path is
the same as the last one and the edges on the
path are distinct.
Graph Terminology
 Cycle:
◦ circular sequence of alternating vertices and edges.
◦ each edge is preceded and followed by its
endpoints (vertices).

 Simple cycle:
◦ cycle such that all its vertices and edges are
distinct.
Graph Terminology
 Examples:
◦ C1 = (V, X, Y, W, U, V) is a simple cycle.
◦ C2 = (U, W, X, Y, W, V, U) is a cycle that is not simple.
Graph Terminology
 If each edge in the graph carries a value, then
the graph is called weighted graph.
 A weighted graph is a graph G = (V, E, W),

where each edge, e ∈E is assigned a real


valued weight, W(e).
Graph Terminology
 A complete graph is a graph with an edge
between every pair of vertices.
 That is a graph is called complete graph if

every vertex is adjacent to every other


vertex.
Graph Terminology
 Complete undirected graph has all possible
edges.
◦ Examples:
Graph Terminology
 Subgraph: subset of vertices and edges
forming a graph.
 A graph G = (V , E ) is a subgraph of a
s s s
graph G = (V, E) if Vs ⊆V, Es ⊆E, and Es
⊆Vs×Vs.
Graph Terminology
 Connected graph: any two vertices are
connected by some path.
◦ An undirected graph is connected if, for every
pair of vertices u and v there is a path from u to
v.
Complete vs connected
 A complete graph is connected.
 A connected graph may not be (and often is

not) complete.
 A complete graph is a simple graph that

contains exactly one edge between each


pair of distinct vertices.
 In a connected graph, there is a path

between any given pair of vertices


Graph Terminology
 Connected component: maximal connected
subgraph. E.g., the graph below has 3
connected components
Graph Terminology
 A directed graph is strongly connected if
every two vertices are reachable from each
other.
 A directed graph is strongly connected if

there is a path between all pairs of


vertices
Graph Terminology
 A strongly connected digraph is a directed
graph in which it is possible to reach any
node starting from any other node by
traversing (navigating) edges in the
direction(s) in which they point
Graph Terminology
 A directed  graph  G  is strongly
connected if:
 For any two vertices  u  and  v :

◦ There is a directed path  u  →  v ,     and     


◦ There is a directed path  v  →  u
Connected vs Strongly connected
vs Complete graphs
 Connected  is usually associated with
undirected graphs (two way edges): there is
a path between every two nodes.
 Strongly connected  is usually associated

with directed graphs (one way edges): there is


a route between every two nodes.
 Complete graphs are undirected graphs

where there is an edge between every pair of


nodes.
Graph Terminology
 A digraph is weakly connected if when
considering it as an undirected graph it is
connected. I.e., for every pair of distinct
vertices u and v there exists
an  undirected  path (potentially running
opposite the direction on an edge)
from u to v. (Next Slide)
Graph Terminology
Graph Terminology
 Symmetric directed graphs are directed
graphs where all edges are bidirected (that
is, for every arrow that belongs to the
digraph, the corresponding inversed arrow
also belongs to it).
 A symmetric digraph is a directed graph,

G = (V, E), such that if (u, v) ∈E, then (v, u)


∈E.
 (Next Slide)
Graph Terminology
Graph Terminology
 Tree - connected undirected graph without
cycles.
 Forest - collection of trees.
Number of Edges
 Degree of a vertex:
◦ X has degree 5.
 Parallel edges:
◦ h and i are parallel edges
Number of Edges
 Undirected Graph:
◦ Since edge(u, v) is the same as edge(v, u), the
number of edges in a complete undirected
graph is n(n-1)/2.
◦ Number of edges in an undirected graph is
≤ n(n-1)/2.
Number of Edges
 Directed Graph:
 Since edge(u, v) is not the same as edge(v, u),

the number of edges in a complete directed


graph is n(n-1).
 Number of edges in a directed graph is

≤ n(n-1).
Vertex Degree
 Degree (of a vertex): # of adjacent vertices.
 degree(2) = 2
 degree(5) = 3
 degree(3) = 1
Sum of Vertex Degrees
(Undirected Graph)
 Sum of degrees of all vertex = 2|E|
 Since adjacent vertices each count the

adjoining edge, it will be counted twice.


In-Degree of a Vertex
(Digraph)
 In-degree is number of incoming edges.
 In-degree(2) = 1 and in-degree(8) = 0.
Out-Degree of a Vertex (Digraph)
 Out-degree is number of outbound edges.
 Out-degree(2) = 1 and out-degree(8) = 2.
Sum of In- and Out-Degrees
(Digraph)
Sum of In- and Out-Degrees
(Digraph)
 Each edge contributes 1 to the in-degree of
some vertex and 1 to the out-degree of some
other vertex.

 Sum of in-degrees = sum of out-degrees = |E|,


where |E| is the number of edges in the digraph.
The sum of all the out-degrees and all the in-
degrees is, of course, twice the number of edges (by the
hand-shaking lemma). Therefore, the sum of the out-
degrees is equal to the number of edges.
Sum of In- and Out-Degrees
(Digraph)
Sum of In- and Out-Degrees
undirected
Sum of In- and Out-Degrees
undirected
Hand-shaking
lemma Example.
deg(A) =3
deg(B) =3
deg(C) =1
deg(D) =3
deg(E) =3
deg(F) =4
deg(G) =2
deg(H) =3
Sum = 22
|E|= 22/2 = 11
Graphs
 We will typically express running times in
terms of
◦ |V| = number of vertices, and
◦ |E| = number of edges,
◦ If |E| ≈ |V|2 the graph is dense. (intensive)
◦ If |E| ≈ |V| the graph is sparse. (scattered)
 If you know you are dealing with dense or
sparse graphs, different data structures may
make sense.
Graphs
Graphs
Graph Representation
 Adjacency Matrix
 Adjacency Lists
Graph Representation
 Adjacency Matrix
 Assume V = {1, 2, …, n}.
 An adjacency matrix represents the graph as

a n × n matrix A:

{
 A[i, j] = 1 if edge (i, j) ∈E (or weight of edge).
= 0 if edge (i, j) ∉ E.
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
 How much storage does the adjacency matrix
require?
◦ Answer: O(|V|2). The time complexity to calculate
the number of edges in a graph whose information
is stored in form of an adjacency matrix n*n (in bits)
 What is the minimum amount of storage
needed by an adjacency matrix representation
of an undirected graph with 4 vertices ?
 Answer : (|V|-1) *|V|/2  6 bits
◦ Undirected graph → matrix is symmetric
◦ No self-loops → don’t need diagonal
Adjacency Matrix

minimum amount of storage


needed by an adjacency matrix
representation of an undirected
graph with 4 vertices  6 bits

But adjacency matrix require


needs n*n bits to represent the
matrix in the storage which
means … it needs 16 bits in
real !
Adjacency Matrix
 The adjacency matrix is a dense
representation.
◦ Usually too much storage for large graphs.
◦ But can be very efficient for small graphs.
◦ If graph is unweighted, there is an additional
advantage in storage for the adjacency matrix
presentation; rather than using one word of
computer memory for each matrix entry, the
adjacency matrix uses only one bit per
entry.
◦ Prefer to be a directed graph.
Adjacency list
 Most large interesting graphs are sparse.
 For this reason the adjacency list is often a

more appropriate representation.


 Adjacency list: for each vertex v ∈V, store a

list of vertices adjacent to v.


 Adjacency list for vertex i is a linear list of

vertices adjacent from vertex i.


 Each adjacency list is a chain.
Adjacency List

In the example  10
Adjacency List
Adjacency List
 How much storage is required?
◦ The degree of a vertex v = # incident edges.
◦ Directed graphs have in-degree and out-degree.
 For directed graphs:
◦ # of items in adjacency lists is Σ out-degree(v) = |
E|.
◦ Takes Θ(V + E) storage (Why?)
 For undirected graphs:
◦ # items in adjacency lists is Σ degree(v) = 2|E|.
◦ Also, Θ(V + E) storage.
 So: Adjacency lists take O(V + E) storage.
Adjacency Matrix vs. List

Next Slide
Adjacency Matrix vs. List

Figure (b) : The graph Θ(V + E) storage = 6 + 8 = 14


Figure (a) : The graph O(V 2) storage = 6 ^ 2= 36

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