Social Media Influence Analyzer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Social Media Influence Analyzer:

Evaluating Reach using BFS and DFS

Explanations:
BFS (Breadth-First Search) and DFS (Depth-First Search) are algorithms used to
traverse or search through graphs or trees in different orders.

BFS (Breadth-First Search):

• Meaning:
• BFS is a graph traversal algorithm that explores all the vertices of a
graph level by level.
• It starts from a source vertex and visits all its neighbors before moving
on to the next level of vertices.
• Implementation in Python:
• Utilizes a queue data structure.
• It is often used to find the shortest path in an unweighted graph.

DFS (Depth-First Search):

• Meaning:
• DFS is a graph traversal algorithm that explores as far as possible along
one branch before backtracking.
• It explores deeper into the graph structure before moving to the next
branch.
• Implementation in Python:
• Can be implemented using recursion or an explicit stack.
• Useful for topological sorting, detecting cycles in a graph, and solving
problems on strongly connected components.
FlowChart :
Coding
from collections import defaultdict

class InfluenceAnalyzer:
def __init__(self):
self.graph = defaultdict(list)

def add_edge(self, u, v):


self.graph[u].append(v)

def bfs(self, start_node):


visited = set()
queue = [start_node]
visited.add(start_node)
while queue:
node = queue.pop(0)
print(node, end=' ')

for neighbor in self.graph[node]:


if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)

def dfs(self, start_node):


visited = set()

def dfs_util(node):
print(node, end=' ')
visited.add(node)

for neighbor in self.graph[node]:


if neighbor not in visited:
dfs_util(neighbor)

dfs_util(start_node)

# Example usage
ia = InfluenceAnalyzer()
ia.add_edge('Vijay', 'ajith')
ia.add_edge('vijay', 'suresh')
ia.add_edge('ajith', 'dinesh')
ia.add_edge('suresh', 'vivek')
ia.add_edge('dinesh', 'vignesh')
ia.add_edge('vivek', 'vignesh')
print("BFS:")
ia.bfs('Vijay')
print("\nDFS:")
ia.dfs('ajith')

Output:
BFS:
Vijay ajith dinesh vignesh
DFS:
ajith dinesh vignesh

You might also like