DSA
DSA
DSA
Traversal algorithms
Define a graph G = (V, E) by defining a pair of ◦Denote vertices with labels
sets:
A B
◦V = a set of vertices
Representation:
◦E = a set of edges
◦Represent vertices with circles, perhaps
containing a label
Edges: ◦Represent edges with lines between circles
◦ Each edge is defined by a pair of vertices
◦ An edge connects the vertices that define it Example:
◦ In some cases, the vertices can be the same ◦ V = {A,B,C,D}
◦ E = {(A,B),(A,C),(A,D),(B,D),(C,D)}
Vertices: A
◦Vertices also called nodes
Contd.
.
Kinds of Choose the kind
graphs
1. Weighted(edges have a weight) or unweighted (edges have no weight) required for problem
◦ Weighted : typically shows cost of traversing and determined by data
5
◦ Example: weights are distances between cities A B
2. Directed or undirected
◦ Undirected Graphs:
◦ each edge can be traversed in either direction
◦no implied direction on edge between nodes
◦ Directed Graphs:
◦ each edge can be traversed only in a specified direction
◦ an edge is an unordered pair
Contd.
.
3. Cyclic or acyclic
A Cyclic graph contains cycles
◦Example: roads (normally)
A directed graph is strongly connected if every pair of vertices has a path between them, in both directions
Contd.
.
The degree of a node is the number of edges the node is used to define
Example
◦Degree 2: B and C
◦Degree 3: A and D
A and D have odd degree, and B and C have even degree
Can also define in-degree and out-degree
◦In-degree: Number of edges pointing to a node
◦Out-degree: Number of edges pointing from a node
Graphs: Terminology Involving
Paths
Path: sequence of vertices in which each pair of successive vertices is connected by an edge
Cycle: a path that starts and ends on the same vertex
Simple path: a path that does not cross itself
◦That is, no vertex is repeated (except first and last)
◦Simple paths cannot contain cycles
It explores all the nodes by going forward or It uses the idea of backtracking
It can be implemented using stack
Steps:
1. Pick a node and push all its adjacent nodes into a stack.
2. Pop a node from the stack and push all its adjacent nodes into a stack.
3. Repeat this process until the stack is empty or you meet a goal.
Example DFS implementation using stack.
G
Breadth First
Search
An algorithm for traversing or searching tree or graph data structures.
It explores all the nodes at the present depth before moving on to the nodes at the next depth level.
It can be implemented using a queue.
Steps:
1. Pick a node and enqueue all its adjacent nodes into a queue.
2. Dequeue a node from the queue, mark it as visited and enqueue all its adjacent nodes into a queue.
3. Repeat this process until the queue is empty or you meet a goal.
Shortest path problem
The problem of finding a path between two vertices (or nodes) in a graph
The sum of the weights of its constituent edges is minimized.
All pair shortest problem
◦ Single execution of the algorithm will find the lengths (summed
weights) of shortest paths between all pairs of vertices.
◦ Warshall’s Algorithm and Floyd’s Algorithm
◦The algorithm can also be used for finding the transitive closure of a relation (R)
of directed graph.
• given : directed graph G = ( V, E ),
construct : create an n × n matrix D = ( dij ) of shortest
path distances i.e., dij = ( vi , vj )
solution : Execute the algorithm n times, one for every vertex as the
source.
example
Adjacency Matrix
Digraph
Exampl
e
Given a digraph. Find the transitive closure using floyd’ss algorithm.
Distance Matrix
Digraph
Trees and Minimum Spanning
Trees
Tree: undirected, connected graph with no cycles
◦Example : If G=(V, E) is a tree, how many edges in
G?
Types of graph
Kruskal’s
algorithm
It forms a tree with every vertex in it.
The sum of the weights is the minimum among all the spanning trees that can
be formed from this graph.
Steps for Kruskal’s algorithm :
1. First sort all the edges from the lowest weight to highest.
2. Take edge with the lowest weight and add it to the spanning tree. If the
cycle is created, discard the edge.
3.Keep adding edges like in step 1 until all the vertices are considered.
Example : Find MST for the given graph:
Contd.
1. .choose the edge with the least weight which is
2-4.
2. choose the next shortest edge 2-3.
3. choose next edge with the shortest edge and
that does not create a cycle i.e. 0-3
4. choose the shortest edge so that it doesn’t
form a cycle. This is 0-1.
Note: if more than one edge with the same weight, pick a random one.
Contd.
.
Topological
Sort
A directed graph and returns an array of the nodes where each node appears before all the nodes it
points to.
sorting is possible if the graph is directed acyclic graph.
The ordering of the nodes in the array is called a topological ordering.
Applications:
Scheduling jobs from the given dependencies among jobs
Instruction Scheduling
Determining the order of compilation tasks to perform in make files
Data Serialization
Contd.
.
Example:
Find the number of different topological orderings possible for the given graph
1. specify in-degree of all vertex
Contd.
.
2. least in-degree vertex and its associated edges are removed. Update in-degree of all remaining
vertices.
3. Vertices with the same least in-degree. Continue with all cases separately by applying step 2.
4. repeat step 2 for all cases
5. repeat step 2 for all cases
Since in previous step, 2 vertices with the least in-degree. So, 2 options are possible. Any of the two
vertices may be taken first.
This is applicable for both cases.
Contd.
.
Solution :
The given graph can have 4 different topological ordering:
123456
123465
132456
132465