In this following presentation, one can get an idea for solving a common celebrity problem. It has general algorithm along with pseudocode, the code and the methods of solving it. It also discusses the time complexity in each step for better graphical representation.
What are the advantages and disadvantages of membrane structures.pptx
The Celebrity Problem Graph
1. THE
CELEBRITY
PROBLEM
Presented By: Group 1
Ipsita Raha – Dibyendu Banik – Arnab Chatterjee – Arijit Dhali
11500120054 – 11500320062 – 11500320076 – 11500320078
SOLVING BY GRAPHICAL METHOD
2. TABLE OF CONTENTS
Disscussing Problem
& Strategy
01
Graphical
Approach
02
Pseudocode &
Algorithm
03
Complexity Analysis
& Result analysis
04
Program Code &
Other Approaches
05
Conclusion &
References
06
3. CELEBRITY
PROBLEM
A celebrity among a group of n people is a
person who knows nobody but is known
by everybody else. The task is to identify a
celebrity by only asking questions to
people of the following form: “Do you
know this person?
4. STRATEGY
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0
1
2
3
4
0 1 2 3 4
Person
Let us understand the solution using two sample cases where we consider a matrix of person [ i , j ]
(for person i knows j or not)
Hence, 2 is the Celebrity here
Case 1: 0 0 1 0 0
0 0 1 0 0
0 1 0 0 0
0 0 1 0 0
0 0 1 0 0
0
1
2
3
4
0 1 2 3 4
Person
Case 2:
Hence, there is no celebrity here
From these examples,
We can conclude the conditions :
• Condition 1:
Every other person knows the celebrity
• Condition 2:
Celebrity doesn’t knows no other person
5. 01
GRAPHICAL
APPROACH
● Model the solution using graphs
● Initialize indegree and outdegree of every vertex as 0.
● If A knows B, draw a directed edge from A to B, increase
indegree of B and outdegree of A by 1.
● Construct all possible edges of the graph for every possible
pair [ i , j ].
● Now there are nC2 pairs.
● For atleast 1 celebrity present in the party, there will be one
sink node in the graph with outdegree of zero and indegree
of N-1
6. PSEUDOCODE
1. Create two arrays indegree and outdegree, to store the indegree and outdegree
2. Run a nested loop, the outer loop from 0 to n and inner loop from 0 to n.
3. For every pair i, j check if i knows j then increase the outdegree of i and indegree of j
4. For every pair i, j check if j knows i then increase the outdegree of j and indegree of i
5. Run a loop from 0 to n and find the id where the indegree is n-1 and outdegree is 0
7. ALGORITHM WITH TIME COMPLEXITY
findCelebrity (n):
indegree -> [0 for x from 0 to n]
outdegree -> [0 for x from 0 to n]
for i <- 0 to n -1
for j <- 0 to n -1
x = knows( i , j )
outdegree[ i ] + = x
indegree[ j ] + = x
for i <- 0 to n -1
if indegree[ i ] = n - 1 and outdegree[ i ] = 0
return i
return -1
Cost Times
C1
n
C2
C3
C4
𝑗=0
𝑛−1
𝑡𝑗
C5
C6
C7
C8
n
C9
C10
C11 1
8. CONTINUATION
knows(a, b):
return MATRIX[ a ][ b ]
Main:
id <- findCelebrity(n)
if id = -1:
No celebrity
else
Celebrity Present, print id
Cost Times
C12 1
Here, tj is the number of times the while loop is executed.
Also, MATRIX = [ [ 0, 0, 1, 0 ], and N = 4 ( Person in party )
[ 0, 0, 1, 0 ],
[ 0, 0, 0, 0 ],
[ 0, 0, 1, 0 ] ]
Cost Times
C13 1
C14
1
C15
C16
1
C17
9. COMPLEXITY ANALYSIS
● Time Complexity : O(n2)
A nested loop is run traversing the array, SO the
time complexity is O(n2)
= (C1+C2+C3+C8+C9+C10 )(n) + (C4+C5+C6+C7)(n2) +
C11 + C12 + C13 + C14 + C15 + C16 + C17
= O(n2)
● Space Complexity: O(n)
Since extra space of size n is required.
10. RESULT ANALYSIS 1
Let us consider the previous case 1, and create tables for
indegree and outdegree
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0
1
2
3
4
0 1 2 3 4
Person
Indegree Outdegree
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
1
1
2
1
1
3
1
4
In this table we can see, person 2 has
Indegree = 4 = (5 – 1) or (n – 1)
Outdegree = 0
∴ Person 2 is the celebrity
11. RESULT ANALYSIS 2
Let us consider the previous case 2, and create tables for
indegree and outdegree
Indegree Outdegree
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
1
1
2
1
1
3
1
4
In this table we can see, person 2 has
Indegree = 5 ≠ (5 – 1) or (n – 1)
Outdegree = 1
∴ Returns -1 which means no celebrity
0 0 1 0 0
0 0 1 0 0
0 1 0 0 0
0 0 1 0 0
0 0 1 0 0
0
1
2
3
4
0 1 2 3 4
Person 1
1
14. CONCLUSION
In this term paper we solved the given problem using
graphical method.
For graphs to be computationally useful, they have to be
conveniently represented in programs.
However it is simple to implement, easy and fast to tell if
a pair ( i , j ) is an edge: simply check if A[ i ][ j ] is 1 or 0.
No matter how few edges the graph has, the matrix takes
O(n2) in memory
16. CREDITS: This presentation template was created by Slidesgo,
including icons by Flaticon, infographics & images by Freepik
THANK
YOU
For your kind attention towards our
presentation
Group 1