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

11-Graphs-Intro

The document provides an overview of graphs, defining them as mathematical structures consisting of vertices and edges, and discusses their applications in various fields such as maps and communication networks. It outlines key terminologies related to graphs, including directed and undirected graphs, paths, cycles, and types of connectivity. Additionally, it covers methods for graph representation in memory, specifically through adjacency matrices and adjacency lists.
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)
6 views

11-Graphs-Intro

The document provides an overview of graphs, defining them as mathematical structures consisting of vertices and edges, and discusses their applications in various fields such as maps and communication networks. It outlines key terminologies related to graphs, including directed and undirected graphs, paths, cycles, and types of connectivity. Additionally, it covers methods for graph representation in memory, specifically through adjacency matrices and adjacency lists.
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/ 33

Graphs

Data Structures and Algorithms


Outline
 Non-Linear Data Structures
 Graphs
 Intro
 Application
 Terminologies
 Representation
 Adjacency List
 Adjacency Matrix

2
Graph
 A graph is a way of representing relationships between pairs of objects.
 It is a set of objects, called vertices, together with a collection of pairwise
connections between them, called edges

3
Graph
 Graph is a mathematical structure that is defined as G= (v, e), where v is a set
of vertices{v1, v2, …vn} and e is a set of edges {e1,e2,e3,…em}
 Where edge e is an ordered pair of two vertices, represents a connection between two vertices
 Graph can be directed, or undirected
 Example:
 V = {A, B, C, D, E, F, G}
 E = { {A, B}, {A, D}, {A, E}, {B, C}, {B, D}, {B, E}, {C, E}, {C, F}, {D, E} }
 Then the graph G=(V,E) is: B B
 |V|= 7 A A
 C C
|E|=9

D E F G D F G
E
4
Applications
 Maps

 Communication Networks Social networks

5
Applications
 Flight routes

 Web graphs Maze (games)

6
Terminologies
 Directed Graph  Un-Directed Graph
 Also called digraph  Edge has no direction and considered
 Every edge has a direction, an incoming as two-ways
edge is not equal to outgoing edge  Vertex order is not important
V

U X Z

 Vertex order is important

7
Terminologies
 End Points  Parallel Edges
 Vertices at both ends of an edge
 Edges with same end points
 Incident Edge
 h and i are parallel edges
 If vertex is an end point of edge, edge is incident on
that vertex
 a is incident on u and v
 Adjacent/Neighbor Vertices V
a b
 Vertices that are end points of same edge h
 u, v and w are adjacent d
 Self Loop U X Z j
 Node connected to itself c e i
 Z is in self loop and j is self edge
 S W g
Degree of Node
 Number of incident edges f
 U has degree 2, X has degree 5

Y
Self edge is considered twice, so z has degree 4
8
Terminologies
 In a directed graph we can distinguish between
 Incoming Edges
 Directed edges for which the given vertex is destination
 c is incoming edge of U
 Outgoing Edges
 Directed edges for which the given vertex is origin
 a is outgoing edge of U V
a b
 In-Degree of Vertex h
 Number of incoming edges d
 Out-Degree of Vertex U X Z
 Number of outgoing edges c e
 Source Vertices i
 Vertices with an in-degree of zero W g
 w is source vertex
 Sink Vertices f
 Vertices with an out-degree of zero Y
 v is sink vertex
9
Terminologies
 Path
 A path between two vertices is sequence of alternating vertices and edges, where each
successive vertex is connected.

V
V

U X Z
U X Z

W W

Y Y

10
Terminologies
 Simple Path
 A path with distinct nodes
 One of the possible paths from u to v is u-w-v
 Cycle
 A path that starts and finish at same vertex V
V

U X Z
U X Z

W W

Y Y

11
Terminologies
 Reachability of Vertex
 A vertex v is reachable from other vertex if there exists a path from other vertex to
vertex v
 In undirected graph, edge is considered as 2-ways, so every vertex is reachable in following
example.

V V

U X Z U X Z

 U , V and X are not reachable from Z all vertices are reachable from each other

12
Terminologies
 Connected Graph  Disconnected Graph
 Each vertex is reachable from every other  A graph that is not connected
vertex, in other words there exists a path
from each vertex to every other vertex

V V

U X Z U X Z

13
Terminologies
 Strongly Connected Graph  Weakly Connected Graph
 If there exists a directed path between  If there exists a path between each pair
each pair of vertices of vertices which ignores direction

V V

U X Z U X Z

14
Terminologies
 Completely Connected Graph
 If there exists an edge between each pair of vertices
 Directed  Undirected
V

V
U X

U X
X

 Maximum Edges?  Maximum Edges


 |E|=|V|*|V-1| =O(|V|2)  |E|=|V|*|V-1|/2= O(|V|2)
15
Terminologies
 Sub Graph V
 A graph that is consists of subset of vertices and edges of G

 U X Z
Two possible sub-graphs
V

U X Z
U X

 Following is not valid sub-graph of G, why?


 Look at definition of sub-graph

U X Z

16
Terminologies
 Simple Graph
 A graph with no parallel and self edges
V

U X Z

17
Terminologies
 Weighted Graph  Non-Weighted Graph
 Weights on edge means cost or distance
between end points of edge

18
Terminologies
 Path Length
 Sum of weights of edges on path from one vertex to other.
 Length of path between u and w is 2
 Length of path between u and y is 3

V
There are two paths from u to x 1 2
2
 Path u-w-x has length 6 and path u-w-y-x has length 5
2
 Shortest Path U X Z
 Path with minimum length 2 4 1
 From u to x is 5
W 2
 From u to v is 4
1
Y

19
Terminologies
 Tree
 An undirected graph G is tree if it fulfills any of the following condition:
 G has V-1 edges and no cycles
 G has V-1 edges and is connected
 G is connected, but removing any edge disconnects it
 G is acyclic, but adding any edges creates a cycle
 Exactly one simple path between each pair of vertices in G

V V

U X Z U X Z

20
Terminologies
 Directed Acyclic Graph (DAG)
 Directed graph without cycles
V

U X Z

 Following graph is not DAG, as it contains a cycle


U X Z

21
Terminologies
 Directed Acyclic Graph (DAG) B D
 A directed graph without cycles
A
 Applications: C E
 The parse tree constructed by a compiler to execute sequential statements
 Dependency graphs for task scheduling
 Dependency graphs between classes formed by inheritance relationships in object-oriented
programming languages
 Information categorization systems, such as folders in a computer
 Course pre-requisites CS16 CS141 CS242
CS15

CS17 CS32
CS18 CS123 CS224

22
Graph ADT
 Common methods for Graph ADT can be:
 numVertices()
 vertices()
 numEdges()
 edges()
 outgoingEdges(v)
 incomingEdges(v)
 getEdge(v1, v2)
 endVertices(e)
 opposite(v, e)
 insertVertex(value)
 insertEdge(v1, v2, value)
 removeVertex(v)
 removeEdge(e)

 Run time depends upon underlying implementation


23
Memory Representation
 There are multiple ways to represent a graph in memory:
 Adjacency Matrix
 Adjacency List
 Edge List
 Adjacency Map

 Assume following example for all representations


V1 V3

V5

V2 V4

24
Adjacency Matrix
 The adjacency matrix of a graph G = (V, E) is a |V | × |V | matrix E, where each
entry Eij is equal to 1 if there exists an edge e = (vi, vj ) ∈ E and 0 otherwise.
 1 and 0 can also be replaced with true/false.
 Vertex list itself is stored in 1D array
0 1 2 3 4
“V1” “V2” “V3” “V4” “V5”

V1 V2 V3 V4 V5
V1 0 1 1 0 0
V1 V3 V2 1 0 1 1 0

V5
V3 1 1 0 1 1
V4 0 1 1 0 1
V2 V4 V5 0 0 1 1 0

25
Adjacency Matrix
 If graph is directed?
 Then entry Eij is equal to 1 if there exists an edge e = from vi to vj and 0 otherwise.
V1 V2 V3 V4 V5
V1 0 1 1 0 0
V1 V3
V2 0 0 1 0 0
V3 0 0 0 1 1 V5

V4 0 1 0 0 0
V2 V4
V5 0 0 0 1 0

 Weighted Graph:
 1 and 0 are replaced with respective weights
 0 or -1 presents no edge

26
Example

27
Example
 If vertices labels can be used as index, then separate 1D array is not needed to
store them, edge matrix would be enough.
0 1 2 3 4
0 0 1 1 0 0
0 2
1 0 0 1 0 0
2 0 0 0 1 1 4
3 0 1 0 0 0
1 3
4 0 0 0 1 0

28
Adjacency Matrix
 Vertex list and Edge list are given
 0 1 2 3 4
Running Time?
 “V1” “V2” “V3” “V4” “V5”
Get a vertex’s out-edges
 Get a vertex’s in-edges V1 V2 V3 V4 V5
 Decide if some edge exists
V1 0 1 1 0 0
 Insert an edge
V2 0 0 1 0 0
 Delete an edge

V3 0 0 0 1 1
Inset a vertex
 V4 0 1 0 0 0
Remove a vertex
 V5 0 0 0 1 0
Memory
 O(|V|2)
 Good for?
 Dense graphs
 Edges are close to |V|2
29
Adjacency List
 Each vertex is associated with its list of connected vertices.
 There can be different ways to implement it. Simplest one is using vertex numbers as
array index, where each index would hold a pointer to linked list of connected vertices.

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

4 2 3

30
Adjacency List
 Another idea can be to store edges list inside of vertex and storing vertices in array.
 Edge list then can be a list of vertices itself or list of indices of connected vertices
 It also can be label of vertices

1 “V1” 2 3

2 “V2” 1 3 4 V1 V3

3 “V3” 1 2 4 5 V5

4 “V4” 2 3 5 V2 V4

5 “V5” 3 4

 Here edge list is storing indicies


31
32
Adjacency List
 Running Time?
 1 “V1” 2 3
Get a vertex’s out-edges
 Get a vertex’s in-edges 2 “V2” 1 3 4
 Decide if some edge exists
 3 “V3” 1 2 4 5
Insert an edge
 Delete an edge 4 “V4” 2 3 5
 Inset a vertex
 5 “V5” 3 4
Remove a vertex
 Memory
 O(|V|+|E|)
 Good for?
 Sparse
 Edges are significantly less than |V| 2
33

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