UNIT IV
GRAPHS
Definitions - Graphs
• A graph is an abstract data type (ADT) that
consists of a set of objects that are connected
to each other via links. These objects are
called vertices and the links are called edges.
• Usually, a graph is represented as G = {V, E},
where G is the graph space, V is the set of
vertices and E is the set of edges. If E is empty,
the graph is known as a forest.
Adjacency List
The adjacency list is a list
of the vertices directly
connected to the other
vertices in the graph.
• Types of graph
• There are two basic
types of graph −
• Directed Graph
• Undirected Graph
Topological Sort
• Topological sorting for
Directed Acyclic Graph
(DAG) is a linear ordering
of vertices such that for
every directed edge u-v,
vertex u comes
before v in the ordering.
• Note: Topological Sorting
for a graph is not possible
if the graph is not a DAG.
Shortest-Path Algorithms
Dijkstra’s Algorithm
Given a graph and a source vertex in the graph, find
the shortest paths from the source to all vertices in the
given graph.
Fundamentals of Dijkstra's Algorithm
• Dijkstra's Algorithm begins at the node we select (the
source node), and it examines the graph to find the shortest
path between that node and all the other nodes in the
graph.
• The Algorithm keeps records of the presently acknowledged
shortest distance from each node to the source node, and it
updates these values if it finds any shorter path.
• The procedure continues until all the nodes in the graph
have been included in the path. In this manner, we have a
path connecting the source node to all other nodes,
following the shortest possible path to reach each node.
Kruskal’s algorithm
• In Kruskal’s algorithm, sort all edges of the
given graph in increasing order. Then it keeps
on adding new edges and nodes in the MST if
the newly added edge does not form a cycle.
• It picks the minimum weighted edge at first
and the maximum weighted edge at last. Thus
we can say that it makes a locally optimal
choice in each step in order to find the optimal
solution. Hence this is a Greedy Algorithm.
• Sort all the edges in non-decreasing order of
their weight.
• Pick the smallest edge. Check if it forms a cycle
with the spanning tree formed so far. If the
cycle is not formed, include this edge. Else,
discard it.
• Repeat step#2 until there are (V-1) edges in
the spanning tree.
Prim's Algorithm
• Prim's Algorithm is a greedy algorithm that is used
to find the minimum spanning tree from a graph.
Prim's algorithm finds the subset of edges that
includes every vertex of the graph such that the sum
of the weights of the edges can be minimized.
• Prim's algorithm starts with the single node and
explores all the adjacent nodes with all the
connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got
selected.
Algorithm
• First, we have to initialize an MST with the
randomly chosen vertex.
• Now, we have to find all the edges that
connect the tree in the above step with the
new vertices. From the edges found, select the
minimum edge and add it to the tree.
• Repeat step 2 until the minimum spanning
tree is formed.
Step 1: Create a forest F in such a way that every vertex of the graph is
a separate tree.
Step 2: Create a set E that contains all the edges of the graph.
Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not
spanning
Step 4: Remove an edge from E with minimum weight
Step 5: IF the edge obtained in Step 4 connects two different trees, the
n add it to the forest F
(for combining two trees into one tree).
ELSE
Discard the edge
Step 6: END
Depth First Traversal (or DFS)
• Depth First Traversal (or DFS) for a graph is
similar to Depth First Traversal of a tree. The
only catch here is, that, unlike trees, graphs
may contain cycles (a node may be visited
twice). To avoid processing a node more than
once, use a boolean visited array. A graph can
have more than one DFS traversal.
EXAMPLE
Biconnectivity
• An undirected graph is called Biconnected if there are two
vertex-disjoint paths between any two vertices. In a
Biconnected Graph, there is a simple cycle through any two
vertices.
• For a graph with more than two vertices, the above
properties must be there for it to be Biconnected.
Or in other words:
A graph is said to be Biconnected if:
• It is connected, i.e. it is possible to reach every vertex from
every other vertex, by a simple path.
• Even after removing any vertex the graph remains connected.
• A connected graph is
Biconnected if it is
connected and doesn’t
have any
Articulation Point. We
mainly need to check
two things in a graph.
• The graph is connected.
• There is not articulation
point in graph.
Euler circuits
• Eulerian Path is a path in a
graph that visits every edge
exactly once. Eulerian
Circuit is an Eulerian Path
that starts and ends on the
same vertex.
Eulerian Cycle:
• Eulerian Cycle: An undirected
graph has Eulerian cycle if
following two conditions are
true.
• All vertices with non-zero
degree are connected. We don’t
care about vertices with zero
degree because they don’t
belong to Eulerian Cycle or Path
(we only consider all edges).
• All vertices have even degree.
Eulerian Path:
• Eulerian Path: An undirected
graph has Eulerian Path if
following two conditions are true.
• Same as condition (a) for Eulerian
Cycle.
• If zero or two vertices have odd
degree and all other vertices have
even degree. Note that only one
vertex with odd degree is not
possible in an undirected graph
(sum of all degrees is always even
in an undirected graph)
Applications of graphs.
• Google maps uses graphs for building transportation systems, where
intersection of two(or more) roads are considered to be a vertex and the road
connecting two vertices is considered to be an edge, thus their navigation
system is based on the algorithm to calculate the shortest path between two
vertices.
• In Facebook, users are considered to be the vertices and if they are friends
then there is an edge running between them. Facebook’s Friend suggestion
algorithm uses graph theory. Facebook is an example of undirected graph.
• In World Wide Web, web pages are considered to be the vertices. There is an
edge from a page u to other page v if there is a link of page v on page u. This is
an example of Directed graph. It was the basic idea behind
Google Page Ranking Algorithm.

TREE ADT, TREE TRAVERSALS, BINARY TREE ADT

  • 1.
  • 2.
    Definitions - Graphs •A graph is an abstract data type (ADT) that consists of a set of objects that are connected to each other via links. These objects are called vertices and the links are called edges. • Usually, a graph is represented as G = {V, E}, where G is the graph space, V is the set of vertices and E is the set of edges. If E is empty, the graph is known as a forest.
  • 4.
    Adjacency List The adjacencylist is a list of the vertices directly connected to the other vertices in the graph. • Types of graph • There are two basic types of graph − • Directed Graph • Undirected Graph
  • 5.
    Topological Sort • Topologicalsorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u-v, vertex u comes before v in the ordering. • Note: Topological Sorting for a graph is not possible if the graph is not a DAG.
  • 6.
    Shortest-Path Algorithms Dijkstra’s Algorithm Givena graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph.
  • 7.
    Fundamentals of Dijkstra'sAlgorithm • Dijkstra's Algorithm begins at the node we select (the source node), and it examines the graph to find the shortest path between that node and all the other nodes in the graph. • The Algorithm keeps records of the presently acknowledged shortest distance from each node to the source node, and it updates these values if it finds any shorter path. • The procedure continues until all the nodes in the graph have been included in the path. In this manner, we have a path connecting the source node to all other nodes, following the shortest possible path to reach each node.
  • 8.
    Kruskal’s algorithm • InKruskal’s algorithm, sort all edges of the given graph in increasing order. Then it keeps on adding new edges and nodes in the MST if the newly added edge does not form a cycle. • It picks the minimum weighted edge at first and the maximum weighted edge at last. Thus we can say that it makes a locally optimal choice in each step in order to find the optimal solution. Hence this is a Greedy Algorithm.
  • 9.
    • Sort allthe edges in non-decreasing order of their weight. • Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not formed, include this edge. Else, discard it. • Repeat step#2 until there are (V-1) edges in the spanning tree.
  • 10.
    Prim's Algorithm • Prim'sAlgorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized. • Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.
  • 11.
    Algorithm • First, wehave to initialize an MST with the randomly chosen vertex. • Now, we have to find all the edges that connect the tree in the above step with the new vertices. From the edges found, select the minimum edge and add it to the tree. • Repeat step 2 until the minimum spanning tree is formed.
  • 12.
    Step 1: Createa forest F in such a way that every vertex of the graph is a separate tree. Step 2: Create a set E that contains all the edges of the graph. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning Step 4: Remove an edge from E with minimum weight Step 5: IF the edge obtained in Step 4 connects two different trees, the n add it to the forest F (for combining two trees into one tree). ELSE Discard the edge Step 6: END
  • 13.
    Depth First Traversal(or DFS) • Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain cycles (a node may be visited twice). To avoid processing a node more than once, use a boolean visited array. A graph can have more than one DFS traversal.
  • 14.
  • 15.
    Biconnectivity • An undirectedgraph is called Biconnected if there are two vertex-disjoint paths between any two vertices. In a Biconnected Graph, there is a simple cycle through any two vertices. • For a graph with more than two vertices, the above properties must be there for it to be Biconnected. Or in other words: A graph is said to be Biconnected if: • It is connected, i.e. it is possible to reach every vertex from every other vertex, by a simple path. • Even after removing any vertex the graph remains connected.
  • 16.
    • A connectedgraph is Biconnected if it is connected and doesn’t have any Articulation Point. We mainly need to check two things in a graph. • The graph is connected. • There is not articulation point in graph.
  • 17.
    Euler circuits • EulerianPath is a path in a graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path that starts and ends on the same vertex.
  • 18.
    Eulerian Cycle: • EulerianCycle: An undirected graph has Eulerian cycle if following two conditions are true. • All vertices with non-zero degree are connected. We don’t care about vertices with zero degree because they don’t belong to Eulerian Cycle or Path (we only consider all edges). • All vertices have even degree. Eulerian Path: • Eulerian Path: An undirected graph has Eulerian Path if following two conditions are true. • Same as condition (a) for Eulerian Cycle. • If zero or two vertices have odd degree and all other vertices have even degree. Note that only one vertex with odd degree is not possible in an undirected graph (sum of all degrees is always even in an undirected graph)
  • 19.
    Applications of graphs. •Google maps uses graphs for building transportation systems, where intersection of two(or more) roads are considered to be a vertex and the road connecting two vertices is considered to be an edge, thus their navigation system is based on the algorithm to calculate the shortest path between two vertices. • In Facebook, users are considered to be the vertices and if they are friends then there is an edge running between them. Facebook’s Friend suggestion algorithm uses graph theory. Facebook is an example of undirected graph. • In World Wide Web, web pages are considered to be the vertices. There is an edge from a page u to other page v if there is a link of page v on page u. This is an example of Directed graph. It was the basic idea behind Google Page Ranking Algorithm.