0% found this document useful (0 votes)
4 views31 pages

14 Graph Intro

Graph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph intro

Uploaded by

nausheenshah175
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)
4 views31 pages

14 Graph Intro

Graph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph introGraph intro

Uploaded by

nausheenshah175
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/ 31

Graphs

ORD
SFO

LAX DFW

Graphs 1
Outline and Reading
Graphs (§6.1)
◼ Definition
◼ Applications
◼ Terminology
◼ Properties
◼ ADT
Data structures for graphs (§6.2)
◼ Edge list structure
◼ Adjacency list structure
◼ Adjacency matrix structure

Graphs 2
Graphs: Motivation (6.1)
Suppose we want to visualize flight routes in a country
A natural representation is with a graph
Nodes – cities
Edges – flights between cities
Edge labels - distance

Graphs 3
Graph Formal Definition
A graph is a pair (V, E), where
◼ V is a set of nodes, called vertices

◼ E is a collection of pairs of vertices, called edges

In this example
V ={a, b, c, d, f}
E ={(a, c), (b, c), (c, f), (b, d), (d, f), (c, d)}

Graphs 4
Edge Types flight
ORD AA 1206 PVD
Directed edge (u,v)
◼ ordered pair of vertices (u,v)

◼ first vertex u is the origin flight


◼ second vertex v is the ORD PVD
AA 1207
destination
◼ Asymmetric relation

◼ e.g., a flight

◼ (u,v) and (v,u) are different BOB


edges
Undirected edge
◼ unordered pair of vertices (u,v)
SAM PAT
◼ symmetric relation

◼ e.g., network of friends

◼ (u,v) and (v,u) are same edges


MARY
Graphs 5
Graph Types
Directed graph / digraph
◼ all the edges are directed

◼ e.g., flight network

Undirected graph
◼ all the edges are undirected

◼ e.g., friends network

Graphs 6
Applications
cslab1a cslab1b

Transportation networks math.brown.edu


◼ Highway network
◼ Flight network cs.brown.edu
Computer networks
◼ Routing algorithms brown.edu

Computer Vision qwest.net


att.net
▪ Image pixels are graph
nodes, neighboring pixels
connected by edges cox.net
John

Paul
David

Graphs 7
Terminology
End vertices (or endpoints)
◼ two vertices joined by an
edge are called the end
vertices of the edge
◼ U and V are the
endpoints of edge a V
An edge is said to be incident a b
h j
on a vertex if the vertex is one
of the edge’s endpoints U d X Z
◼ Edges a, d, and b are
incident on V c e i
Adjacent vertices: 2 vertices W g
are adjacent if there is an
edge between them f
◼ U and V are adjacent
Y
• Degree of a vertex - deg(v):
number of edges incident on v
X has degree 5
Graphs 8
Terminology(Directed graph)
The outgoing edges of a vertex are the
directed edges whose origin is that vertex.
(1,2) (1,3) of vertex 1

The incoming edges of a vertex are the


directed edges whose destination is that
vertex. (1,2) of vertex 2

The in-degree of a vertex v is the


number of the incoming edges of v, and is
denoted as indeg(v). indeg(1) = 0

The out-degree of a vertex v is the


number of the outgoing edges of v, and is
denoted as outdeg(v). outdeg(1) = 2

Graphs 9
Terminology
Parallel edges
◼ edges that have the same V
endpoints a b
h j
◼ h and i are parallel edges
U d X Z
Self-loop c e i
◼ edge whose endpoints coincide

◼ j is a self-loop
W g
f
Simple graph: Y
◼ If a graph does not have
parallel edges and self loops, it
is said to be a simple graph.

Graphs 10
Terminology
Path
◼ Sequence of alternating
vertices and edges
◼ Begins with a vertex

◼ Ends with a vertex


V
◼ Each edge is preceded and a b
followed by its endpoints P1
Simple path
U d X Z
◼ path such that all its P2 h
vertices are distinct c e
Examples W g
◼ P1=(V,b,X,h,Z) is a simple
path f
◼ P2=(U,c,W,e,X,g,Y,f,W,d,V) Y
is a path that is not simple
Graphs 11
Terminology
Cycle
◼ Path with same start and
end vertices
V
Simple cycle a b
◼ Cycle such that all its
d
vertices are distinct U X Z
except the first and last C2 h
one e C1
c
Examples W g
◼ C1=(V,b,X,g,Y,f,W,c,U,a,V)

is a simple cycle f
Y
◼ C2=(U,c,W,e,X,g,Y,f,W,d,V,

a,U) is a cycle that is not


simple
Graphs 12
Connected graph
A graph is connected if there exist a path between
any two vertices

Graphs 13
Complete graph
◼ A graph where there exists an edge from every
node to every other node
◼ No. edges: In undirected complete graph it is n(n-

1)/2 (n: no. of vertices)


◼ In directed complete graph it is n (n-1)

Graphs 14
Subgraph
A subgraph of a graph G is a graph whose vertices
and edges are subsets of the vertices and edges of
G, respectively

Sub graph (H)


Graph (G)

Graphs 15
Subgraph

G1 is a subgraph of G2 and G3

Graphs 16
Spanning Subgraph
A spanning subgraph of G is a subgraph of G that
contains all the vertices of the graph G

Graphs 17
Connected components
If a graph G is not connected, its maximal connected
subgraphs are called the connected components of G

Graphs 18
Spanning Tree
A spanning tree of a graph is a connected subgraph of
the graph with all the vertices of the graph and minimum
number of edges possible.
In the context of graph, a tree has no root. So referred as
free tree. The regular tree DS with a root is referred as
rooted tree.

graph tree
Graphs 19
Notation
Properties n
m
number of vertices
number of edges
Property 1 deg(v) degree of vertex v
If G is a graph with m Example
edges, then 2
◼ n = 4
v deg(v) = 2m
◼ m= 6
1 3 ◼ deg(1) = 3
Proof:
◼ deg(2) = 3
• In the above summation
each edge (u,v) is counted ◼ deg(3) = 3
twice 4
◼ deg(4) = 3
• Thus, the total contribution
of the edges to the degrees
of the vertices is twice the
number of edges deg(1) +deg(2) + deg(3) + deg(4) = 12= 2*6

Graphs 20
Notation
n number of vertices
Properties m number of edges
deg(v) degree of vertex v
Property 2
If G is a directed graph with Example
m edges, then 2
◼ n = 4
σ𝒗 ε 𝑮 𝒊𝒏𝒅𝒆𝒈 𝒗 =
◼ m= 6

෍ 𝒐𝒖𝒕𝒅𝒆𝒈 𝒗 = 𝒎
1 3 indeg(1) = 1 ; outdeg(1) = 2
𝒗ε𝑮
Proof: indeg(2) = 1 ; outdeg(2) = 2
• In a digraph an edge (u,v) indeg(3) = 2 ; outdeg(3) = 1
4
contributes one unit to indeg(4) = 2 ; outdeg(4) = 1
outdegree and one unit to
indegree indeg(1) +indeg(2) + indeg(3) + indeg(4) = 6
• Thus, the total contribution of outdeg(1) +outdeg(2) + outdeg(3) +
the edges to the outdegree of outdeg(4) = 6
the vertices is equal to the
number of edges and similarly
for indegree Graphs 21
Properties
Property 3
Let G be a simple graph with n vertices and m edges
If G is undirected, then 𝑚 ≤ 𝑛 (𝑛 − 1)/2
If G is directed, then 𝑚 ≤ 𝑛 (𝑛 − 1)
Proof:
◼ Suppose G is undirected the maximum degree of a vertex in G
is n – 1
◼ By property 1 we have 2m ≤ 𝒏 𝒏 − 𝟏

◼ Similarly if G is directed the maximum degree(indegree or out


degree) of a vertex in G is n – 1
◼ By property 2 we have m ≤ 𝒏 𝒏 − 𝟏

Graphs 22
Operations on Graphs
Accessor methods Update methods
◼ aVertex() ◼ insertVertex(o)
◼ incidentEdges(v) ◼ insertEdge(v, w, o)
◼ endVertices(e) ◼ insertDirectedEdge(v, w, o)
◼ isDirected(e) ◼ removeVertex(v)
◼ origin(e) ◼ removeEdge(e)
◼ destination(e) Generic methods
◼ opposite(v, e) ◼ numVertices()
◼ areAdjacent(v, w) ◼ numEdges()
◼ vertices()
◼ edges()

Graphs 23
Operations on Graphs

Graphs 24
Operations on Graphs

Graphs 25
Operations on Graphs

Graphs 26
Data Structures for
Representing Graphs (6.2)
Adjacency list
Adjacency matrix
For a graph G with n vertices and m edges
◼ adjacency list representation uses O (n + m) space

◼ adjacency matrix representation uses O (n2) space

Graphs 27
Adjacency-list representation
The adjacency-list representation of a graph G =
(V, E) consists of
◼ An array Adj of |V| lists, one for each vertex in V
◼ For each 𝑢 ∈ 𝑉, the adjacency list 𝐴𝑑𝑗[𝑢] contains all the
vertices 𝑣 such that there is an edge 𝑢, 𝑣 ∈ 𝐸
The amount of memory required is O(V + E)

Graphs 28
Example

Graphs 29
Adjacency matrix
The adjacency-matrix representation of a graph G
consists of a |V| x |V| matrix A = aij such that
1 𝑖𝑓 𝑖, 𝑗 ∈ 𝐸
𝑎𝑖𝑗 = ቊ
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
The adjacency matrix of a graph requires O(n2)
memory
|V| ➔Cardinality/No. elements in V

Graphs 30
Example

Graphs 31

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