Data Structures & Algorithms ppt 4
Data Structures & Algorithms ppt 4
A weighted Graph is a Graph where the edges have values. The weight
value of an edge can represent things like distance, capacity, time, or
probability.
A connected Graph is when all the vertices are connected through edges
somehow. A Graph that is not connected, is a Graph with isolated (disjoint)
subgraphs, or single isolated vertices.
• A directed Graph, also known as a digraph, is when the edges between
the vertex pairs have a direction. The direction of an edge can represent
things like hierarchy or flow.
GRAPH TERMINOLOGY
Cycle: A path that starts and ends at the same vertex without traversing any edge more than once.
Subgraph: A graph formed from a subset of the vertices and edges of another graph.
Connected Graph: An undirected graph is connected if there is a path between every pair of
vertices.
Disconnected Graph: A graph is disconnected if it is not connected, i.e., if there are at least two
vertices with no path between them.
Complete Graph: A graph in which there is an edge between every pair of vertices.
GRAPH TERMINOLOGY cont.
Path: A sequence of edges that allows you to go from one
vertex to another.
B
D E
G
Understanding how a Graph can be traversed is important for
understanding how algorithms that run on Graphs work.
The two most common ways a Graph can be traversed are:
• Depth First Search (DFS)
• Breadth First Search (BFS)
• DFS is usually implemented using a Stack or recursion (which utilizes
the call stack), while BFS is usually implemented using a Queue.
How it works:
B
D E
Result: D, A, C, B, F, E, G
The DFS traversal starts in vertex D and marks it as visited. Then, for every
new vertex visited, the traversal method is called recursively on all adjacent
vertices that have not been visited yet. So when vertex A is visited in the
diagram above, vertex C or vertex E (depending on the implementation) is the
next vertex where the traversal continues.
IMPLEMENTATION
# Using a Python dictionary to act as an adjacency list
graph = {
'5' : ['3','7’],
'3' : ['2', '4’],
'7' : ['8’],
'2' : [],
'4' : ['8’],
'8' : []
}
PYTHON visited = set() # Set to keep track of visited nodes of graph.
IMPLEMENTATION
def dfs(visited, graph, node): #function for dfs
if node not in visited:
print (node) visited.add(node)
for neighbour in graph[node]: dfs(visited, graph,
neighbour)
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
APPLICATIONS OF DEPTH-FIRST SEARCH
• Web crawlers
• Model checking
• Testing if graph is bipartite
• Finding strongly connected components
BREATH-FIRST SEARCH (BFS)
Breadth First Search visits all adjacent vertices of a vertex before visiting neighboring
vertices to the adjacent vertices. This means that vertices with the same distance from the
starting vertex are visited before vertices further away from the starting vertex are visited.
How it works:
2. For each vertex taken from the queue, visit the vertex, then put all unvisited adjacent
vertices into the queue.
D E
Result: D, A, C, E, B, F, G
BFS traversal visits vertices the same distance from the starting
vertex, before visiting vertices further away. So for example,
after visiting vertex A, vertex E and C are visited before visiting
B, F and G because those vertices are further away.
• Breadth First Search traversal works this way by putting all
adjacent vertices in a queue (if they are not already visited),
and then using the queue to visit the next vertex.
IMPLEMENTATION
graph = {
'5' : ['3’, '7’],
'3' : ['2', '4’],
'7' : ['8’],
'2' : [],
'4' : ['8’],
'8' : []
}
visited = [] # List for visited nodes.
queue = [] #Initialize a queue
PYTHON
IMPLEMENTATION def bfs(visited, graph, node): #function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node m =
queue.pop(0) print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour) queue.append(neighbour)
# Driver Code
print("Following is the Breadth-First Search") bfs(visited,
graph, '5') # function calling
APPLICATIONS FOR BFS
1 2 3 4 5
Graph Finds all reachable Work on both Can be used for Time
Traversal: Both BFS vertices: Both directed and searching: Both Complexity: In the
and DFS are used to algorithms can find undirected algorithms can be worst-case scenario,
traverse or explore all the vertices graphs: Both used to find a both algorithms have
all nodes in a graph. reachable from a algorithms can be specific node or a time complexity of
starting vertex. used on both element within a O(V + E), where V is
directed and graph. the number of
undirected graphs. vertices (nodes) and
E is the number of
edges in the graph.
… Is a well-known algorithm in computer
science used to find the shortest paths
from a starting node to all other nodes in
a graph, especially when the graph has a
non-negative edge weights.
DIJKSTRA'S
ALGORITH Purpose:
M
The primary function of Dijkstra's
algorithm is to determine the shortest
paths (or distances) from a designated
source node to every other node within a
graph.
Dijkstra's algorithm finds the shortest paths
from a starting node (source) to all other
nodes in a graph,
including: