Design & Analysis of Algorithms
Lecture#09
Graph
Lecture Contents
Graph
Graph Terminologies
Graph Representations (Adjacency Matrix, Adjacency Link List)
Graph Traversals (Depth First, Breadth First)
Spanning Tree
Minimum Spanning Tree
 Kruskalls’ Algorithm
 Prim’s Algorithm
Shortest Path
 Dijkstra’s Algorithm
 Bellman Ford Algorithm
 A* Algorithm
Graph
A graph G (V, X) consists of two sets V & X. V is a set of
vertices & X is a set of Edges
Every edge e ϵ X joins two vertices in V
Graph G = (V, X)
where V = Set of vertices
AND X = set of edges
AND X  V x V
Example:
G = (V, X)
V = {A, B, C, D, E, F}
X = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
Directed Graph (Digraph)
A directed graph is a graph in which all edges are directed. Directed
graph is also called a digraph.
G = (V, E)
V = {A, B, C, D, F, G}
E = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
Undirected Graph
A undirected graph is a graph in which all edges are bidirectional.
In an undirected graph G(V,E),
∀e ϵ E
e1 (u, v) = e2 (u, v)
Connected Graph
A graph is connected if there is path between every two nodes of
that graph.
Complete Graph
A graph is complete if there is edge between every two nodes of that
graph.
Total number of edges in complete graph are given as:
𝑇𝑜𝑡𝑎𝑙 𝐸𝑑𝑔𝑒𝑠 =
𝑛(𝑛 − 1)
2
Degree, In Degree, Out Degree
Degree:
Number of edges connecting a vertex is called the
degree of node.
Degree of vertex A is 6
In Degree:
Number of edges pointing into a vertex is called its in
degree
In Degree of vertex A is 2
Out Degree:
Number of edges going out of a vertex is called its out
degree
Out Degree of vertex A is 4
Adjacency
Two nodes are said to be adjacent if they are connected through an edge.
In the example below nodes B&C, C&D, D&E are adjacent to each other.
Adjacent Nodes
Weighted Graph
A graph in which every edge has some weight (numeric
value) associated with it.
It refers to the effort while going from one vertex to other
using this edge Weighted Graph
Unweighted Graph
A graph having no weight value associated with
any of its edge is called an unweighted graph
Unweighted Graph
Reachable Node
A node is reachable from another node if there exists a path between them.
Path exists between A & E
Multigraph, Pseudo Graph
Multigraph
A graph is said to be multigraph if it allows more than
one paths between two nodes
Pseudo Graph
A graph is pseudo graph if it allows if it allows self
loops
Multigraph
Pseudo Graph
Reachable Node
A node is reachable from another node if
there exists a path between them.
For given graph paths exist between:
A & B
A & D
A & F
B & F
C & F
Path exists between A & E
Path
A path of length k is sequence of v1, v2, v3, …., vk of vertices such that (vi, vi+1) for i
= 0, 1, 2, …, k-1 is an edge of graph.
1. ABCD is a path
2. ABD is not a path
Cycle
A cycle is a path starting and ending at same vertex
ABCA makes a cycle in this graph
Hamiltonian Cycle
A Hamiltonian Cycle is a cycle in an undirected graph which visits every vertex
of graph and also returns to starting vertex.
Hamiltonian Cycle:
A, B, C, D, E, F, G, H, A
Eulerian Cycle
An Eulerian Cycle of a connected directed / undirected graph is a cycle that
traverses each vertex of a graph exactly once. There may be one vertex that is
visited more than once.
Eulerian Cycle:
A, B, C, D, E, F, C, G back to A
Subgraph
A subgraph H of graph G is a graph that has its vertices and edges as
subsets of vertices and edges of G respectively.
Graph G
V = {A, B, C, D, E, F, G}
Edges={(A,B), (A,G), (B,C), (B, G), (C,D), (C,F), (C,G), (D,E),
(D,F), (E,F)}
Subgraph H
V = {A, B, C, G}
Edges={(A,B), (A,G), (B,C), (B, G), (C,G)}
Tree
A connected graph without any cycles is call a tree graph or simply a tree.
For a tree graph, there is a unique path between every two nodes of the graph
Tree
Graph Representation
Graphs are represented through two different ways:
1. Adjacency Matrix
2. Adjacency List
Graph Adjacency Matrix Representation
Say a graph has V={1,2,3,…,n} vertices then graph is represented by a
NXN matrix A such that:
A[i, j] = 1 if there is edge between nodes i & j
= 0 otherwise
Graph G
Graph G 1 2 3 4
1 0 0 1 0
2 0 0 1 1
3 1 1 0 1
4 0 1 1 0
Space Complexity: ϴ|V2|
Graph Adjacency Matrix Representation
Adjacency matrix is dense representation i.e. too much space required for large
number of vertices
Can be very efficient for small graphs
Graph Adjacency List Representation
For each vertex, a list of adjacent vertices (neighbors) is
maintained
For the given graph:
Adj[1] = {3}
Adj[2]={3, 4}
Adj[3]={1, 2, 4}
Adj[4]={2, 3}
A possible variation can be to store the list of incoming edges
instead of outgoing edges
Graph G
Graph Adjacency List Representation
If a graph is directed then sum of lengths of all adjacency lists is equal to the
number of edges in graph
If the graph is undirected then this number is doubled
Graph G
Adjacency List for G
Graph Traversals
Graphs are traversed using two different methods:
1. Breadth First Traversal
2. Depth First Traversal
A Typical Graph Traversal
For a connected graph, pick one node as root node and visit all nodes of the
graph in some order
Starting from root we iterate for each node of the graph
During every iteration we visit the node and capture the neighboring nodes of
node in hand
Keep repeating until all nodes are visited
For graphs with disconnected nodes, we may need to visit subgraphs
containing all nodes disconnected with root node separately
Spanning Tree (ST)
“A spanning tree is a subset of Graph G, which has all the vertices covered
with minimum possible number of edges”
A spanning tree can contain no cycle
A spanning tree cannot be disconnected
A graph can have multiple spanning trees
Spanning tree T1
Graph G
Spanning tree T2
Spanning Tree (ST)
“A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges”
Number of vertices for all spanning trees of a graph are same
Number of edges for all spanning trees of a graph are same
All spanning trees form a minimally connected graph i.e. one
takes away one edge from ST and it is no more a spanning tree
Adding one edge to ST will introduce a cycle i.e. ST is maximally
acyclic
Graph G
Spanning tree T1
Spanning Tree (ST) … Applications
Network Planning & Design
Network Routing Protocols
Cluster Analysis
Approximation Algorithms for NP-hard Problems
Image Registration & Segmentation Algorithms
Minimax Process Control
etc
Minimum Spanning Tree (MST)
“Minimum spanning tree is subset of edges of a connected,
weighted undirected graph that connects all vertices
together and bears the minimum possible total edge
weight”
Minimum Spanning Tree is also called Minimum Weight
Spanning Tree
MST is a tree so it does not contain any cycle
Graph K
Spanning Tree T1
Spanning Tree T2

Graph terminology and algorithm and tree.pptx

  • 1.
    Design & Analysisof Algorithms Lecture#09 Graph
  • 2.
    Lecture Contents Graph Graph Terminologies GraphRepresentations (Adjacency Matrix, Adjacency Link List) Graph Traversals (Depth First, Breadth First) Spanning Tree Minimum Spanning Tree  Kruskalls’ Algorithm  Prim’s Algorithm Shortest Path  Dijkstra’s Algorithm  Bellman Ford Algorithm  A* Algorithm
  • 3.
    Graph A graph G(V, X) consists of two sets V & X. V is a set of vertices & X is a set of Edges Every edge e ϵ X joins two vertices in V Graph G = (V, X) where V = Set of vertices AND X = set of edges AND X  V x V Example: G = (V, X) V = {A, B, C, D, E, F} X = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
  • 4.
    Directed Graph (Digraph) Adirected graph is a graph in which all edges are directed. Directed graph is also called a digraph. G = (V, E) V = {A, B, C, D, F, G} E = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
  • 5.
    Undirected Graph A undirectedgraph is a graph in which all edges are bidirectional. In an undirected graph G(V,E), ∀e ϵ E e1 (u, v) = e2 (u, v)
  • 6.
    Connected Graph A graphis connected if there is path between every two nodes of that graph.
  • 7.
    Complete Graph A graphis complete if there is edge between every two nodes of that graph. Total number of edges in complete graph are given as: 𝑇𝑜𝑡𝑎𝑙 𝐸𝑑𝑔𝑒𝑠 = 𝑛(𝑛 − 1) 2
  • 8.
    Degree, In Degree,Out Degree Degree: Number of edges connecting a vertex is called the degree of node. Degree of vertex A is 6 In Degree: Number of edges pointing into a vertex is called its in degree In Degree of vertex A is 2 Out Degree: Number of edges going out of a vertex is called its out degree Out Degree of vertex A is 4
  • 9.
    Adjacency Two nodes aresaid to be adjacent if they are connected through an edge. In the example below nodes B&C, C&D, D&E are adjacent to each other. Adjacent Nodes
  • 10.
    Weighted Graph A graphin which every edge has some weight (numeric value) associated with it. It refers to the effort while going from one vertex to other using this edge Weighted Graph
  • 11.
    Unweighted Graph A graphhaving no weight value associated with any of its edge is called an unweighted graph Unweighted Graph
  • 12.
    Reachable Node A nodeis reachable from another node if there exists a path between them. Path exists between A & E
  • 13.
    Multigraph, Pseudo Graph Multigraph Agraph is said to be multigraph if it allows more than one paths between two nodes Pseudo Graph A graph is pseudo graph if it allows if it allows self loops Multigraph Pseudo Graph
  • 14.
    Reachable Node A nodeis reachable from another node if there exists a path between them. For given graph paths exist between: A & B A & D A & F B & F C & F Path exists between A & E
  • 15.
    Path A path oflength k is sequence of v1, v2, v3, …., vk of vertices such that (vi, vi+1) for i = 0, 1, 2, …, k-1 is an edge of graph. 1. ABCD is a path 2. ABD is not a path
  • 16.
    Cycle A cycle isa path starting and ending at same vertex ABCA makes a cycle in this graph
  • 17.
    Hamiltonian Cycle A HamiltonianCycle is a cycle in an undirected graph which visits every vertex of graph and also returns to starting vertex. Hamiltonian Cycle: A, B, C, D, E, F, G, H, A
  • 18.
    Eulerian Cycle An EulerianCycle of a connected directed / undirected graph is a cycle that traverses each vertex of a graph exactly once. There may be one vertex that is visited more than once. Eulerian Cycle: A, B, C, D, E, F, C, G back to A
  • 19.
    Subgraph A subgraph Hof graph G is a graph that has its vertices and edges as subsets of vertices and edges of G respectively. Graph G V = {A, B, C, D, E, F, G} Edges={(A,B), (A,G), (B,C), (B, G), (C,D), (C,F), (C,G), (D,E), (D,F), (E,F)} Subgraph H V = {A, B, C, G} Edges={(A,B), (A,G), (B,C), (B, G), (C,G)}
  • 20.
    Tree A connected graphwithout any cycles is call a tree graph or simply a tree. For a tree graph, there is a unique path between every two nodes of the graph Tree
  • 21.
    Graph Representation Graphs arerepresented through two different ways: 1. Adjacency Matrix 2. Adjacency List
  • 22.
    Graph Adjacency MatrixRepresentation Say a graph has V={1,2,3,…,n} vertices then graph is represented by a NXN matrix A such that: A[i, j] = 1 if there is edge between nodes i & j = 0 otherwise Graph G Graph G 1 2 3 4 1 0 0 1 0 2 0 0 1 1 3 1 1 0 1 4 0 1 1 0 Space Complexity: ϴ|V2|
  • 23.
    Graph Adjacency MatrixRepresentation Adjacency matrix is dense representation i.e. too much space required for large number of vertices Can be very efficient for small graphs
  • 24.
    Graph Adjacency ListRepresentation For each vertex, a list of adjacent vertices (neighbors) is maintained For the given graph: Adj[1] = {3} Adj[2]={3, 4} Adj[3]={1, 2, 4} Adj[4]={2, 3} A possible variation can be to store the list of incoming edges instead of outgoing edges Graph G
  • 25.
    Graph Adjacency ListRepresentation If a graph is directed then sum of lengths of all adjacency lists is equal to the number of edges in graph If the graph is undirected then this number is doubled Graph G Adjacency List for G
  • 26.
    Graph Traversals Graphs aretraversed using two different methods: 1. Breadth First Traversal 2. Depth First Traversal
  • 27.
    A Typical GraphTraversal For a connected graph, pick one node as root node and visit all nodes of the graph in some order Starting from root we iterate for each node of the graph During every iteration we visit the node and capture the neighboring nodes of node in hand Keep repeating until all nodes are visited For graphs with disconnected nodes, we may need to visit subgraphs containing all nodes disconnected with root node separately
  • 28.
    Spanning Tree (ST) “Aspanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges” A spanning tree can contain no cycle A spanning tree cannot be disconnected A graph can have multiple spanning trees Spanning tree T1 Graph G Spanning tree T2
  • 29.
    Spanning Tree (ST) “Aspanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges” Number of vertices for all spanning trees of a graph are same Number of edges for all spanning trees of a graph are same All spanning trees form a minimally connected graph i.e. one takes away one edge from ST and it is no more a spanning tree Adding one edge to ST will introduce a cycle i.e. ST is maximally acyclic Graph G Spanning tree T1
  • 30.
    Spanning Tree (ST)… Applications Network Planning & Design Network Routing Protocols Cluster Analysis Approximation Algorithms for NP-hard Problems Image Registration & Segmentation Algorithms Minimax Process Control etc
  • 31.
    Minimum Spanning Tree(MST) “Minimum spanning tree is subset of edges of a connected, weighted undirected graph that connects all vertices together and bears the minimum possible total edge weight” Minimum Spanning Tree is also called Minimum Weight Spanning Tree MST is a tree so it does not contain any cycle Graph K Spanning Tree T1 Spanning Tree T2