Algorithm Design Unit 3
Algorithm Design Unit 3
Unit 3
Graph is a data structure that consists of the following two components:
A finite set of vertices also called nodes.
A finite set of ordered pair of the form (u, v) called edge. The pair is ordered
because (u, v) is not the same as (v, u) in the case of a directed graph(di-graph).
The pair of the form (u, v) indicates that there is an edge from vertex u to vertex
v. The edges may contain weight/value/cost.
Following is an example of an undirected graph with 5 vertices.
Graphs are used to represent many real-life applications: Graphs are used to represent
networks. The networks may include paths in a city or telephone network or circuit
network. Graphs are also used in social networks like linkedIn, Facebook. For example, in
Facebook, each person is represented with a vertex(or node). Each node is a structure and
contains information like person id, name, gender, and locale. See this for more
applications of graph.
In computer science, a graph is a data structure that is used to represent connections or
relationships between objects. A graph consists of a set of vertices (also known as nodes)
and a set of edges (also known as arcs) that connect the vertices. The vertices can represent
anything from cities in a map to web pages in a network, and the edges can represent the
relationships between them, such as roads or links.
A graph can be visualized as a collection of points (vertices) connected by lines (edges),
where each vertex represents a point of interest and each edge represents a connection
between two points. The edges can be directed or undirected, meaning they can either have
a specific direction or be bidirectional. For example, a map of a city may have directed
edges that represent the direction of one-way streets, while a social network may have
undirected edges that represent friendships between individuals.
Representations of Graphs:
The following two are the most commonly used representations of a graph.
Adjacency Matrix
Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice
of graph representation is situation-specific. It totally depends on the type of operations to
be performed and the ease of use.
The graph has two types of traversal algorithms. These are called the Breadth First
Search and Depth First Search.
Breadth-First Search
Traversing or searching is one of the most used operations that are undertaken while working
on graphs. Therefore, in breadth-first-search (BFS), you start at a particular vertex, and the
algorithm tries to visit all the neighbors at the given depth before moving on to the next level
of traversal of vertices. Unlike trees, graphs may contain cyclic paths where the first and last
vertices are remarkably the same always. Thus, in BFS, you need to keep note of all the track
of the vertices you are visiting. To implement such an order, you use a queue data structure
which First-in, First-out approach. To understand this, see the image given below.
Depth-first search
In depth-first-search (DFS), you start by particularly from the vertex and explore as much as
you along all the branches before backtracking. In DFS, it is essential to keep note of the
tracks of visited nodes, and for this, you use stack data structure.
In this case, a topological ordering is just a legitimate task sequence. A topological sort is a
graph traversal in which each node v is only visited after all of its dependencies have been
visited. If the graph contains no directed cycles, then it is a directed acyclic graph. Any DAG
has at least one topological ordering, and there exist techniques for building topological
orderings in linear time for any DAG.
Topological sorting has many applications, particularly in ranking issues like the feedback
arc set. Even if the DAG includes disconnected components, topological sorting is possible.
In computer networks, the shortest path algorithms aim to find the optimal paths between the
network nodes so that routing cost is minimized. They are direct applications of the shortest
path algorithms proposed in graph theory.
Explanation
Consider that a network comprises of N vertices (nodes or network devices) that are
connected by M edges (transmission lines). Each edge is associated with a weight,
representing the physical distance or the transmission delay of the transmission line. The
target of shortest path algorithms is to find a route between any pair of vertices along the
edges, so the sum of weights of edges is minimum. If the edges are of equal weights, the
shortest path algorithm aims to find a route having minimum number of hops.
Design and Analysis - Floyd Warshall Algorithm
Previous Page
Next Page
The Floyd-Warshall algorithm is a graph algorithm that is deployed to find the shortest path
between all the vertices present in a weighted graph. This algorithm is different from other
shortest path algorithms; to describe it simply, this algorithm uses each vertex in the graph as
a pivot to check if it provides the shortest way to travel from one point to another.
Floyd-Warshall algorithm works on both directed and undirected weighted graphs unless
these graphs do not contain any negative cycles in them. By negative cycles, it is meant that
the sum of all the edges in the graph must not lead to a negative number.
Since, the algorithm deals with overlapping sub-problems – the path found by the vertices
acting as pivot are stored for solving the next steps – it uses the dynamic programming
approach.
Floyd-Warshall algorithm is one of the methods in All-pairs shortest path algorithms and it is
solved using the Adjacency Matrix representation of graphs.
Minimum Spanning tree
A minimum spanning tree can be defined as the spanning tree in which the sum of the
weights of the edge is minimum. The weight of the spanning tree is the sum of the weights
given to the edges of the spanning tree. In the real world, this weight can be considered as the
distance, traffic load, congestion, or any random value.
Let's understand the minimum spanning tree with the help of an example.