0% found this document useful (0 votes)
25 views16 pages

Graph 1

Uploaded by

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

Graph 1

Uploaded by

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

Introduction

• A graph is an abstract data structure that is used to implement the graph concept from
mathematics. A graph is basically, a collection of vertices (also called nodes) and edges
that connect these vertices. A graph is often viewed as a generalization of the tree
structure, where instead of a having a purely parent-to-child relationship between tree
nodes, any kind of complex relationships between the nodes can be represented.

Why graphs are useful?


• Graphs are widely used to model any situation where entities or things are related to
each other in pairs; for example, the following information can be represented by
graphs:
• Family trees in which the member nodes have an edge from parent to each of their
children.
• Transportation networks in which nodes are airports, intersections, ports, etc. The
edges can be airline flights, one-way roads, shipping routes, etc.

© Oxford University Press 2014. All rights


Definition
• A graph G is defined as an ordered set (V, E), where V(G) represent the set of
vertices and E(G) represents the edges that connect the vertices.

• The figure given shows a graph with V(G) = { A, B, C, D and E} and E(G) = { (A, B),
(B, C), (A, D), (B, D), (D, E), (C, E) }. Note that there are 5 vertices or nodes and 6
edges in the graph.

A B C

D E

© Oxford University Press 2014. All rights


Definition
• A graph can be directed or undirected. In an undirected graph, the
edges do not have any direction associated with them. That is, if
an edge is drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A. The above figure
shows an undirected graph because it does not gives any
information about the direction of the edges.

• The given figure shows a directed graph. In a directed graph,


edges form an ordered pair. If there is an edge from A to B, then
there is a path from A to B but not from B to A. The edge (A, B) is
said to initiate from node A (also known as initial node) and
terminate at node B (terminal
A node). B C

D E

© Oxford University Press 2014. All rights


Graph Terminology
• Adjacent Nodes or Neighbors: For every edge, e = (u, v) that connects nodes u and v; the
nodes u and v are the end-points and are said to be the adjacent nodes or neighbors.
• Degree of a node: Degree of a node u, deg(u), is the total number of edges containing
the node u. If deg(u) = 0, it means that u does not belong to any edge and such a node is
known as an isolated node.
• Regular graph: Regular graph is a graph where each vertex has the same number of
neighbors. That is every node has the same degree. A regular graph with vertices of
degree k is called a k‑regular graph or regular graph of degree k.

© Oxford University Press 2014. All rights


Graph Terminology
• Path: A path P, written as P = {v0, v1, v2,….., vn), of length n from a node u to v is
defined as a sequence of (n+1) nodes. Here, u = v0, v = vn and vi-1 is adjacent to
vi for i = 1, 2, 3, …, n.
• Closed path: A path P is known as a closed path if the edge has the same end-
points. That is, if v0 = vn.
• Simple path: A path P is known as a simple path if all the nodes in the path are
distinct with an exception that v0 may be equal to vn. If v0 = vn, then the path is
called a closed simple path.
• Cycle: A closed simple path with length 3 or more is known as a cycle. A cycle of length k is
called a k – cycle.
• Connected graph: A graph in which there exists a path between any two of its nodes is called a
connected graph. That is to say that there are no isolated nodes in a connected graph. A
connected graph that does not have any cycle is called a tree.
• Complete graph: A graph G is said to be a complete, if all its nodes are fully connected, that is,
there is a path from one node to every other node in the graph. A complete graph has n(n-
1)/2 edges, where n is the number of nodes in G.

© Oxford University Press 2014. All rights


Graph Terminology
• Labeled graph or weighted graph: A graph is said to be labeled if every edge in the graph is assigned some
data. In a weighted graph, the edges of the graph are assigned some weight or length. Weight of the edge,
denoted by w(e) is a positive value which indicates the cost of traversing the edge.
• Multiple edges: Distinct edges which connect the same end points are called multiple edges. That is, e = {u,
v) and e’ = (u, v) are known as multiple edges of G.
• Loop: An edge that has identical end-points is called a loop. That is, e = (u, u).
• Multi- graph: A graph with multiple edges and/or a loop is called a multi-graph.
• Size of the graph: The size of a graph is the total number of edges in it.

© Oxford University Press 2014. All rights


Graph Terminology

(b) Tree (c) Weighted Graph


(a) Multi-graph
3 4
A B C

e1 A B C

A B e4
2
e2 7 1
e3 D E F

C e6 D B

e5 e7 3

© Oxford University Press 2014. All rights


Directed Graph
• A directed graph G, also known as a digraph, is a graph in which every edge has a direction assigned to it. An
edge of a directed graph is given as an ordered pair (u, v) of nodes in G. For an edge (u, v)-

• The edge begins at u and terminates at v

• U is known as the origin or initial point of e. Correspondingly, v is known as the destination or terminal point of e

• U is the predecessor of v. Correspondingly, v is the successor of u


nodes u and v are adjacent to each other.

© Oxford University Press 2014. All rights


Terminology of a Directed Graph
• Out-degree of a node: The out degree of a node u, written as outdeg(u), is the
number of edges that originate at u.
• In-degree of a node: The in degree of a node u, written as indeg(u), is the number of
edges that terminate at u.
• Degree of a node: Degree of a node written as deg(u) is equal to the sum of in-degree
and out-degree of that node. Therefore, deg(u) = indeg(u) + outdeg(u)
• Source: A node u is known as a source if it has a positive out-degree but an in-degree
= 0.
• Sink: A node u is known as a sink if it has a positive in degree but a zero out-degree.
• Reachability: A node v is said to be reachable from node u, if and only if there exists
a (directed) path from node u to node v.

© Oxford University Press 2014. All rights


Terminology of a Directed Graph
• Strongly connected directed graph: A digraph is said to be strongly connected if and
only if there exists a path from every pair of nodes in G. That is, if there is a path
from node u to v, then there must be a path from node v to u.
• Unilaterally connected graph: A digraph is said to be unilaterally connected if there
exists a path from any pair of nodes u, v in G such that there is a path from u to v or a
path from v to u but not both.
• Parallel/Multiple edges: Distinct edges which connect the same end points are called
multiple edges. That is, e = {u, v) and e’ = (u, v) are known as multiple edges of G.
• Simple directed graph: A directed graph G is said to be a simple directed graph if and
only if it has no parallel edges. However, a simple directed graph may contain cycle
with an exception that it cannot have more than one loop at a given node

© Oxford University Press 2014. All rights


Adjacency Matrix Representation
• An adjacency matrix is used to represent which nodes are adjacent to one another. By
definition, we have learnt that, two nodes are said to be adjacent if there is an edge
connecting them.
• In a directed graph G, if node v is adjacent to node u, then surely there is an edge from u to v.
That is, if v is adjacent to u, we can get from u to v by traversing one edge. For any graph G
having n nodes, the adjacency matrix will have dimensions of n X n.
• In an adjacency matrix, the rows and columns are labeled by graph vertices. An entry aij in the
adjacency matrix will contain 1, if vertices vi and vj are adjacent to each other. However, if the
nodes are not adjacent, aij will be set to zero.

1 if vi is adjacent to vj, that is there is an edge (vi, vj)


aij
0 otherwise

• Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a
Boolean matrix. The entries in the matrix depend on the ordering of the nodes
in G. therefore, a change in the order of nodes will result in a different
adjacency matrix.© Oxford University Press 2014. All rights
Adjacency Matrix Representation

© Oxford University Press 2014. All rights


Adjacency Matrix Representation
From adjacency matrix A1, we have learnt that an entry 1 in the ith row and
jth column means that there exists a path of length 1 from vi to vj. Now
consider, A2, A3 and A4
aij 2 = ∑ aik akj
Any entry aij = 1 if aik = akj = 1. That is, if there is an edge (vi, vk) and (vk, vj). This
implies that there is a path from vi to vj of length 2.
Similarly, every entry in the ith row and jth column of A3 gives the number of
paths of length 3 from node vi to vj.
In general terms, we can conclude that every entry in the ith row and jth
column of An (where n is the number of nodes in the graph) gives the number
of paths of length n from node vi to vj.

© Oxford University Press 2014. All rights


Path Matrix

© Oxford University Press 2014. All rights


Path Matrix
• Now, based on the above calculations, we define matrix B as:
Br = A1 + A2 + A3 + ... + Ar
• An entry in the ith row and jth column of matrix Br gives the
number of paths of length r or less than r from vertex vi to vj.
• The path matrix is used to show whether there exists a simple
path from node vi to vj or not.

© Oxford University Press 2014. All rights


Path Matrix

© Oxford University Press 2014. All rights

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