Graphs
Graphs
Introduction to Graphs
• Definition:
• A graph G=(V,E) consists of:
• Vertices (Nodes): V– set of nodes.
• Edges (Links): E– set of connections between nodes.
• Graph Representation:
• Adjacency Matrix: A square matrix used to represent a graph, where A[i][j]
indicates if there is an edge between vertex i and vertex j.
• Adjacency List: A list where each vertex has a list of adjacent vertices.
Types of graphs
• Undirected Graph:A graph with only undirected edges is said to be
undirected graph.
Types of graphs
• Directed Graph:A graph with only directed edges is said to be directed
graph.
Types of graphs
• A graph in which any V node is adjacent to all other nodes present in
the graph is known as a complete graph. An undirected graph
contains the edges that are equal to edges = n(n-1)/2 where n is the
number of vertices present in the graph. The following figure shows a
complete graph.
Types of graphs
• Cycle Graph:A graph having cycle is called cycle graph. In this case the
first and last nodes are the same. A closed simple path is a cycle.
Types of graphs
• Acyclic Graph:A graph without cycle is called acyclic graphs.
Types of graphs
• Weighted Graph:A graph is said to be weighted if there are some non
negative value assigned to each edges of the graph. The value is equal
to the length between two vertices. Weighted graph is also called a
network.
Key terms in graph
• Outgoing Edge
• A directed edge is said to be outgoing edge on its orign vertex.
• Incoming Edge
• A directed edge is said to be incoming edge on its destination vertex.
• Degree
• Total number of edges connected to a vertex is said to be degree of that
vertex.
Key terms in graph
• Indegree
• Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
• Outdegree
• Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
• Parallel edges or Multiple edges
• If there are two undirected edges to have the same end vertices, and for two directed edges
to have the same
• origin and the same destination. Such edges are called parallel edges or multiple edges.
• Self-loop
• An edge (undirected or directed) is a self-loop if its two endpoints coincide.
• Simple Graph
• A graph is said to be simple if there are no parallel and self-loop edges.
Key terms in graph
• Adjacent nodes
• When there is an edge from one node to another then these nodes are called
adjacent nodes.
• Incidence In an undirected graph the edge between v1 and v2 is incident on
node v1 and v2.
• Walk
• A walk is defined as a finite alternating sequence of vertices and edges,
beginning and ending with vertices, such that each edge is incident with the
vertices preceding and following it.
Key terms in graph
• Path
• A open walk in which no vertex appears more than once is called a
path.
Key terms in graph
• Degree In an undirected graph, the number of edges connected to a
node is called the degree of that node or the degree of a node is the
number of edges incident on it. In the above graph, degree of vertex
v1 is 1, degree of vertex v2 is 3, degree of v3 and v4 is 2 in a
connected graph.
Adjacency Matrix
• In this representation, graph can be represented using a matrix of size
total number of vertices by total number of vertices; means if a graph
with 4 vertices can be represented using a matrix of 4X4 size.
• In this matrix, rows and columns both represent vertices. This matrix
is filled with either 1 or 0. Here, 1 represents there is an edge from
row vertex to column vertex and 0 represents there is no edge from
row vertex to column vertex.
Adjacency Matrix
Adjacency Matrix
For directed graph