Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 64

Chapter 1

GRAPHS: THE BASICS


Introduction
Definition ?
Applications of Graphs?
Characteristics of a Graph, families of graphs?
The most common ways to represent them in a
computer program?
Definition
In algorithms, a graph is a representation of a
set of objects (such as people) and the pairwise
relationships between them (such as
friendships).
Definition
•The objects are called vertices (singular
vertex) or the nodes of the graph.
•The pairwise relationship are called the
edges of the graph.
Definition
We usually denote the vertex and edge
sets of a graph by V and E, respectively,
and sometimes write G = (V,E) to mean
the graph G with vertices V and edges E.
Definition
There are two types of graphs directed and
undirected.
Both types are important and ubiquitous in
applications, so you should know about both of
them.
Definition
 In an undirected graph:
 Each edge corresponds to an unordered
pair {v,w} of vertices, which are called the
endpoints of the edge.
 In an undirected graph, there is no
difference between an edge (v,w) and an
edge (w, v).
Definition
 In a directed graph:
 Each edge (v,w) is an ordered pair, with
the edge traveling from the first vertex v
(called the tail) to the second w (the head);
 There is a difference between an edge
(v,w) and an edge (w, v) in the directed
graph
Introduction
Definition ?
Applications of Graphs?
Elements and the characteristics of a Graph?
The most common ways to represent them in a
computer program?
A Few Applications
Graphs are a fundamental concept, and they show up
all the time in computer science, biology, sociology,
economics, and so on.
A Few Applications
Road networks. When your smartphone’s software
computes driving directions, it searches through a
graph that represents the road network, with vertices
corresponding to intersections and edges
corresponding to individual road segments.
A Few Applications
The World Wide Web. The Web can be modeled as a
directed graph, with the vertices corresponding to individual
Web pages, and the edges corresponding to hyperlinks,
directed from the page containing the hyperlink to the
destination page.
A Few Applications
Social networks. A social network can be represented as a
graph whose vertices correspond to individuals and edges to
some type of relationship. For example, an edge could
indicate a friendship between its endpoints, or that one of its
endpoints is a follower of the other.
A Few Applications
Precedence constraints. Graphs are also useful in problems that lack
an obvious network structure.
For example, imagine that you have to complete a bunch of tasks,
subject to precedence constraints— you’re a first-year university
student, planning which courses to take and in which order. One way
to tackle this problem is to apply the topological sorting algorithm to
the following directed graph: there is one vertex for each course that
your major requires, with an edge directed from course A to course B
whenever A is a prerequisite for B.
Introduction
Definition ?
Applications of Graphs?
characteristics of a Graph, families of graphs?
The most common ways to represent them in a
computer program?
Characteristic of a Graph
Size of a Graph: Two parameters control a graph’s size – the number
of vertices and the number of edges
Notation for Graphs
For a graph G = (V,E) with vertex set V and edge set E:
• n = |V | denotes the number of vertices.
• m = |E| denotes the number of edges.
Order of a Graph : number of vertices of this graph
Characteristic of a Graph
Degree of a vertex:
Neighborhood of a vertex = the set of all neighbors of a vertex of G
= (V, E)  deg ()=||
 Undirected Graph : deg() = # of edges that is one of its endpoint.
(exp: Case of Loop: deg()=2).
for example: deg()=0, it is called isolated vertex.
 Directed Graph : deg+()=number of sorting edges from , deg-()=
number of coming edges to .
Characteristic of a Graph
Degree of a Graph:
Minimum degree of G: 𝛿(𝐺)=min⁡{deg(⁡𝑣) |𝑣∈𝐺}
Maximum degree of G: : ∆(𝐺)=max⁡{deg(⁡𝑣) |𝑣∈𝐺}
What are the order of the graphs G and H shown below. What are the
degrees of their vertices?
Characteristic of a Graph
Walk in a graph G is a sequence 𝑊= whose terms alternate between
vertices and edges such that for 𝑖=1,…,𝑘.
The vertices and the edges in the walk are not necessarily distinct.
If G is a sample graph, we don’t need to mention , we may write the
walk by indicating the vertices only: 𝑊=

Length of walk is the number of edges of the walk=number of


vertices -1
Characteristic of a Graph
 A path is a sequence of vertices with the property that each vertex in the sequence is
adjacent to the vertex next to it.
Closed path is called a cycle
A path that does not repeat vertices is called a simple path.
Length of the path: the number of edges in this path=number of vertices -1
A path from vertex u to vertex v is denoted as , where k is the length of the path.
The distance d from u to v is defined as follows:

if there are no path from u to v


Characteristic of a Graph
Connectivity ?
Undirected Graph
An undirected Graph is connected, it means that
the graph is “in one piece”.
An undirected graph is connected if for all
otherwise it is disconnected.
An undirected graph G is connected if for
every pair of distinct vertices , ; there is a path
from to in G.
Characteristic of a Graph
Connectivity ?
Directed Graph
 A directed Graph is disconnected, if it exists two vertices and such as.
A directed graph G is connected if for every pair of distinct
vertices , ; there is a path from to in G
 A strongly connected directed graph is if there is a directed
path from any vertex in G to any other vertex in G (Within the
graph, it’s possible to get from anywhere to anywhere else) (uv
exists and v u exists)
Characteristic of a Graph
Connectivity ?
Directed Graph
What do we mean by a “strongly connected component” of a
directed graph?
A strongly connected component (SCC) of a directed graph is a
maximal subset of vertices such that there is a directed path
from any vertex in S to any other vertex in S (Within each
component, it’s possible to get from anywhere to anywhere else).
Characteristic of a Graph
Connectivity ?
Directed Graph
For example, how many strongly
connected components does the
graph in shown Figure have?
Characteristic of a Graph
Introduction
Definition ?
Applications of Graphs?
characteristics of a Graph, families of graphs?
The most common ways to represent them in a
computer program?
Families of graphs
Complete Graph
Kn contains exactly one edge between each pair of vertices. The
number of edges in a complete graph Kn is n*(n-1)/2
A simple graph for which there is at least one pair of distinct vertex
not connected by an edge is called non-complete.
Families of graphs
Hamilton Graph
Hamilton cycle: is a cycle that uses all the vertices of the graph with no
repetition of any of these vertices.
Hamilton graph is a graph that has Hamilton cycle
The set of edges and vertices of the path defines a cyclic subgraph =(V, ) of
the whole graph G=(V,E).
Families of graphs
Euler Graph
Euler cycle: is a cycle that uses all the edges of the graph one times (no repetition of edges).
The vertices could be repeated many times.
Euler graph is a graph that has Euler cycle
A connected graph is Euler if the degrees of all vertices are even.
Euler Path: is a path that uses all the edges of the graph one times (no repetition of edges).
The vertices could be repeated many times. A connected graph has Euler path if the number of
vertices with odd degrees are zero or 2.
Families of graphs
Hypercube Graph
An n-dimensional hypercube, or n-cube, denoted by Qn, is a graph
whose vertices representing all the bit strings of length n. any two
adjacent vertices shall have bit strings that differ with exactly one bit
position.
Families of graphs
Tree
A graph is a tree if there is one and only one
path joining any two vertices.
In a tree, every edge is a bridge, i.e. if this edge
is removed then the graph becomes
disconnected.
A tree with vertices must have edges.
A connected graph with vertices and edges
must be a tree.
A tree is also a connected graph without cycle
(Connected and acyclic graph)
Families of graphs
Bipartite Graphs (bigraph)
A graph whose vertex set can be partitioned
into two sets V1 and V2 such that every edge
(u,v)E has uV1 and vV2.
A simple graph is bipartite if and only if it is
possible to assign one of two different colors
to each vertex of the graph so that no two
adjacent vertices are assigned the same color.
Families of graphs
Bipartite Graphs (bigraph)
A graph whose vertex set can be partitioned
into 2 sets V1 and V2 such that every edge
(u,v)E has uV1 and vV2.
A simple graph is bipartite if and only if it is
possible to assign one of two different colors
to each vertex of the graph so that no two
adjacent vertices are assigned the same color.
Families of graphs
Complete Bipartite Graphs (Complete bigraph)
A bigraph is complete bigraph if each vertex from V1 is
connected to all the vertices in V2.
In this case the graph is denoted as Kr,s, where r is the number
of vertices in V1 and s is the number of vertices in V2.
Families of graphs
Subgraph
A subgraph of a graph G = (V, E) is a graph H = (W,
F), where W ⊆ V and F ⊆ E.
Subgraph can be obtained either by :
 vertex deletion (induced subgraph)
 Edge deletion (spanning subgraph)
Families of graphs
Subgraph
Vertex deletion, G-{u5} :
Suppose we delete vertex 5, we
remove all the edges connected to 5
because these edges cannot keep
with one connected endpoint
Edge deletion G\{u2u5, u4u5}:
Suppose we delete the edges u2u5,
u4u5
Introduction
Definition ?
Applications of Graphs?
Characteristics of a Graph, families of graphs?
The most common ways to represent them in a
computer program?
Representing Graphs
To compute things about graphs we first need to represent
them. There is more than one way to represent a graph for
use in an algorithm.
we’ll work primarily with two graph representations:
1. Adjacency List
2. Adjacency Matrix.
Representing a Graph
1. Adjacency lists

For each vertex, we have a


list that linked all the
vertices connected to this
vertex (adjacent to this
vertex).
Adjacency lists specify the
vertices that are adjacent to each
vertex of the graph.
Representing a Graph
1. Adjacency lists
Representing a Graph
1. Adjacency lists
Directed Graph:
listing all the vertices
that are the terminal
vertices of edges starting
at each vertex of the
graph.
Representing a Graph
1. Adjacency lists
Directed Graph:
listing all the vertices
that are the terminal
vertices of edges
starting at each vertex
of the graph.
Representing a Graph
2. Adjacency Matrix
Representing a Graph
2. Adjacency Matrix
Representing a Graph
2. Adjacency Matrix

a b c d e
Representing a Graph
2. Adjacency Matrix

• Note that an adjacency matrix of a graph is


based on the ordering chosen for the vertices.
• n! different adjacency matrices for a graph with
a b c d e n vertices, because there are n! different
orderings of n vertices.
• The degree of a vertex can be deduced from the
adjacency matrix
Representing a Graph
2. Adjacency Matrix
 The adjacency matrix of a simple graph is symmetric.
 Adjacency matrices can also be used to represent undirected graph with
loops and with multiple edges.
◦ A loop at the vertex is represented by a 1 at the position of the adjacency
matrix.
◦ When multiple edges connecting the same pair of vertices and , or
multiple loops at the same vertex, are present, the adjacency matrix is no
longer a zero–one matrix, because the entry of this matrix equals the
number of edges that are associated to { , }.
Representing a Graph
2. Adjacency Matrix
Directed Graph
Representing a Graph
Comparison
How much memory, or space, we need in each
representation?
How long it takes to determine whether a given edge
is in the graph and how long it takes to find the
neighbors of a given vertex?
Representing a Graph
Dense Graph

Adjacency matrix is used


Representing a Graph
Sparse Graph

Adjacency list is used


Annex
Annex
 Implementation code for representing graph
 Sagemath (Open Source mathematics
software)
Sagemath
Sage software is a very useful free mathematical software based on the
Python programming language.
Sage cloud (use Sage online): https://cocalc.com/app
Sage homepage: https://www.sagemath.org/
Library description can be found on
http://doc.sagemath.org/pdf/en/reference/graphs/graphs.pdf
http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_gra
ph.html
Example code in sagemath
A=Matrix([[1,1,1,0], [1,0,1,0], [1,1,0,1], [0,0,1,0]]); A
G=Graph(A)
G.show()
G.size()
G.order()
G=Graph(4,loops=True, multiedges=True, sparse=True)
G.show()
G.add_edges([(0,0),(0,1),(0,2),(1,2),(2,3)])
G.show()
G.has_loops()
G.adjacency_matrix()
A=matrix([[1,1,0],[1,0,1],[0,1,1]]);
A
G=Graph(A)
G.show()
G.vertices()

You might also like