0% found this document useful (0 votes)
24 views29 pages

Unit 7 - Communities - Exercises Solution

The document discusses algorithms for detecting communities and influential nodes in graphs and networks. It provides exercises and solutions for detecting communities using maximal cliques, hierarchical clustering with different linkage criteria, divisive hierarchical clustering using the Girvan-Newman algorithm, and other algorithms like k-cores and min-cut partitioning. For a given graph, it calculates communities using these different algorithms and techniques.

Uploaded by

Aaa Aa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views29 pages

Unit 7 - Communities - Exercises Solution

The document discusses algorithms for detecting communities and influential nodes in graphs and networks. It provides exercises and solutions for detecting communities using maximal cliques, hierarchical clustering with different linkage criteria, divisive hierarchical clustering using the Girvan-Newman algorithm, and other algorithms like k-cores and min-cut partitioning. For a given graph, it calculates communities using these different algorithms and techniques.

Uploaded by

Aaa Aa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Graphs and 

Network Analysis Artificial Intelligence Degree ‐ UAB

UNIT 7 – Algorithms for the detection of communities 
and influential nodes

Exercises

1
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Maximal cliques

• For the graph of the Figure, indicate which communities 
would be detected by a clustering algorithm based on 
maximal cliques.

2
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Maximal cliques ‐ solution

• Cliques:
– Order 1: {1}, {2}, {3}, {4}, {5}, {6}, {7}
– Order 2: {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}, {5,6}
– Order 4: {1,2,3,4}
• Maximal cliques:
– {7}, {5,6}, {1,2,3,4}
• Communities:

3
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Agglomerative hierarchical clustering

• Given the graph of the Figure, With 3 communities, fill the next 


matrix of distances between the three communities, assuming the 
different types of linkage for agglomerative clustering. The values of 
the edges correspond to the distances between the pairs of 
respective nodes.

C1 C2 C3
C1
C2
C3

4
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Agglomerative hierarchical clustering ‐ solution

Single linkage Complete linkage Average linkage


C1 C2 C3 C1 C2 C3 C1 C2 C3
C1 3,9 2,2 C1 4 2,4 C1 3,95 2,3
C2 2 C2 2 C2 2
C3 C3 C3

5
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Agglomerative hierarchical clustering

• Given the graph of the Figure, draw the dendrogram that would be 
generated if we apply a bottom‐up algorithm for detecting 
communities. The values of the edges are the weights (distances 
between the nodes). Use the single linkage criterion to define the 
distance between clusters.

6
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Agglomerative hierarchical clustering ‐ solution

7
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Divisive hierarchical clustering

• Apply the divisive algorithm (e.g. Girvan‐Newman) for detecting 
communities in the graph of the Figure until you find 3 communities. 
You do not need to make calculations numerically for the different 
decision steps (centralities, densities ...), just indicate it intuitively. 

8
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Clustering jeràrquic divisiu ‐ solució


• A divisive algorithm, such as Girvan Newman's, starts from the whole graph, and at each step 
removes an edge. The edge chosen is the one that has the most importance in the graph, in the 
sense of maximum connectivity. That is, if the edge is removed, there is a greater chance that the 
remaining graph will split into two components. For this reason, the edge to be removed is the one 
with the highest interconnection centrality (betweenness centrality) in that iteration. Whenever the 
edge removal splits the subgraph where it appears in two components, the two components can be 
considered communities at that level.
• The following sequence illustrates the process until finding 3 communities. At each step, the edge 
of greater thickness and red color is the one that corresponds to the highest betweenness 
centrality. Since the graph has symmetries, at each step there may be more than one edge with the 
same centrality, therefore the same communities could be obtained in a different order.

9
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection

• Given the graph in the Figure, calculate:


– maximal cliques 8 9
– k‐cores
7
– min‐cut partition
6
– Girvan‐Newman clustering 5

1 4

3
10
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection ‐ solution

• Maximal Cliques:
– {1,5}, {1,4}, {1,2,3}, {5,6,7,8,9} 8 9

7
6
5

1 4

3
11
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection ‐ solution

• K‐cores: 8 9
– 1‐Core: tot el graf
– 1‐Shell: node 4 7

– 2‐Core: Subgraf format per {1,2,3,5,6,7,8,9} 6


5
– 2‐Shell: {1,2,3}
– 3‐Core: {5,6,7,8,9}
– 3‐Shell: {}
– 4‐Core: {5,6,7,8,9}
– 4‐Shell: {5,6,7,8,9} 1 4
– 5‐Core: {}
2

12
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection ‐ solution

• Min‐Cut partition. In each iteration we contract the pair of "closest" nodes 
until we have a graph of as many nodes as the number of partitions we want. 
For 2 partitions, a possible sequence would be:

8 9 8 9 8 9 9
7 7 7 7, 8

6 6 6 6
5 5 5 5

1 4 1,2
4 1,2, 3
4 1,2, 3
4
2
3 3

9
5,7, 8 5,7, 8 5,6,7, 8,9 5,6,7, 8,9

6 6,9

1,2, 3
4 1,2, 3
4 1,2, 3
4 1,2, 3 13
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection ‐ solution

• Girvan‐Newman clustering:
– It would be necessary to calculate the  8 9
centralities of the edges, since the 
algorithm eliminates in each iteration the  7
edge of more centrality. Intuitively, the 
edges of greater centrality would be (1.5)  6
and (1.4). So in 3 iterations Girvan  5
Newman's divisive algorithm would give 3 
communities: {4}, {1,2,3} and {5,6,7,8,9}

1 4

3
14
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling

Four students have to take exams of different disciplines:
– Maria: Maths, Physics, Chemistry
– Marc: Maths, Biology, Programming
– Pol: Maths, Chemistry, Economy
– Claire: Physics, Programming, History 
We have to find a schedule for the exams. What is the minimum number 
of time slots to be provided so that no student has to take two exams 
simultaneously? 

15
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling: solution [graph coloring]

• It can be solved as a graph coloring problem. It consists in labeling with colors 
the nodes of a graph such that no two adjacent nodes are of the same color.
• In graph coloring problem, the chromatic number is the number of required 
colors. 
– The lower bound of the chromatic number is size of the maximum clique (a perfect graph is a 
graph in which the chromatic number equals the size of the maximum clique).
– The upper bound of the chromatic number is one more color than the maximum vertex 
degree.
– We construct a graph where the nodes are the disciplines, and the edges correspond to the 
disciplines that are not compatible in the same time slot.

Maths Biology

Physics History

Chemistry Programming

16
Economy
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling: solution [maximum cliques]


Maths Biology Cliques order 2

Maths Maths Physics

Physics History Physics Chemistry Chemistry

Maths Maths Biology

Chemistry Programming
Biology Programming Programming

Maths Economy
Economy

Economy Chemistry

Physics Physics History


Cliques order 3

Maths Maths History Programming Programming

Physics Chemistry Chemistry Economy

Maths Physics

Biology Programming History Programming 17


Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling: solution [coloring maximum cliques]


Maths Maths

Physics Chemistry Chemistry Economy

The final schedule is:


Maths Physics
• Slot 1: Maths, History
• Slot 2: Chemistry, Programming
• Slot 3: Physics, Economy, Biology
Biology Programming History Programming

And the individual schedules for the students


are:

• Maria: Maths (slot 1), Chemistry (slot 2),


Maths Biology
Physics (slot 3)
• Marc: Maths (slot 1), Programming (slot 2),
Biology (slot 3)
• Pol: Maths (slot 1), Chemistry (slot 2),
Economy (slot 3)
Physics History • Claire: History (slot 1), Programming (slot 2),
Physics (slot 3)

Chemistry Programming

Economy
18
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling

There is a fifth student that has to take exams:
– Maria: Maths, Physics, Chemistry
– Marc: Maths, Biology, Programming
– Pol: Maths, Chemistry, Economy
– Claire: Physics, Programming, History 
– Daniel: Chemistry, Programming, History 
What is the minimum number of time slots to be provided so that no 
student has to take two exams simultaneously? 

19
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling: solution [cliques]


Compatibility graph Maths Biology Cliques order 2

Maths Maths Physics

Physics History Physics Chemistry Chemistry

Maths Maths Biology

Chemistry Programming
Biology Programming Programming

Economy
Maths Economy

Cliques order 3
Economy Chemistry

Maths Maths
Physics Physics History

Physics Chemistry Chemistry Economy


History Programming Programming

Maths Physics
Chemistry History

Biology Programming History Programming


Programming Chemistry

Maths Physics

Clique order 4
Chemistry Programming Chemistry Programming
Physics History
History Physics

Chemistry Programming
Chemistry Programming Chemistry History

20
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Exam scheduling: solution [coloring]


Compatibility graph
Maths Biology

Clique order 4

Physics History
Physics History

Chemistry Programming

Chemistry Programming

Economy The final schedule is:

Cliques order 3 • Slot 1: Maths, History


• Slot 2: Chemistry
Maths Maths • Slot 3: Physics, Biology
• Slot 4: Programming, Economy
Physics Chemistry Chemistry Economy

And the individual schedules for the students


Maths Physics
are:

Biology Programming History Programming


• Maria: Maths (slot 1), Chemistry (slot 2),
Physics (slot 3)
Maths Physics
• Marc: Maths (slot 1), Biology (slot 3),
Programming (slot 4)
Chemistry Programming Chemistry Programming
• Pol: Maths (slot 1), Chemistry (slot 2),
Physics
Economy (slot 4)
History
• Claire: History (slot 1), Physics (slot 3),
Programming (slot 4)
Chemistry Programming Chemistry History
• Daniel: History (slot 1), Chemistry (slot
21 2),
Programming (slot 4)
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Maximum flow

The following graph represents a flow graph. It can be interpreted as a 
network of pipes. The number in the edges indicates the capacity 
(maximum flow) through the pipe (for example, liters/minute). It could 
also be interpreted as a traffic network (cars/minute).

What is the maximum flow capacity between the source and the sink?

5
2 4
6 4

Source 1 9 6 Sink

8 9
3 5
5

22
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Maximum Flow: solution

• The max‐flow min‐cut theorem states that the maximum flow through any 
network from a given source to a given sink is exactly equal to the minimum 
sum of a cut. 
• Therefore, the maximum flow can be calculated by the sum of the weights of 
the edges that define the minimum cut partition in two subgraphs containing 
the source and the sink respectively.
• Some possible cuts:

Cut 2: 5+9+5 = 19
Cut 1: 6+8 = 14
5
2 4
6 4 Cut 3: 5+4 = 9
Cut 4: 6+9+5 = 20
Source 1 9 6 Sink

8 9
3 5
5

23
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Maximum Flow: solution
5
2 4 2
6 4 6 5
4
Source 1 9 6 Sink Source 1 6 Sink
3+4
8
8 9 5 9
3 5 5
5
Contraction of the
biggest edge

2 2
6 5 6 5
Source 1 4 Sink 4
3+4 Source 1+3+4 Sink
8
5 5+6 5+6
5

Min cut: máximum flow


of 5+4 between the
source and the sink

Source 4 Sink
1+2+3+4
5 5+6

24
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection [see Google Colab of this unit]

We have the following information of people (work shift/slot schedule, 
home city and use of public transportation to go to their work).
In COVID‐19 period, we want to detect the risk communities (groups of 
people with high contagion risk).
Can you propose a graph‐based solution? 

id name shift city Public Transport


0 Jaume 1 CityA Yes
1 Raquel 1 CityA Yes
2 Cesc 1 CityC No
3 Anna 1 CityC No
4 Cristina 2 CityD Yes
5 Marta 2 CityD Yes
6 Eva 2 CityB Yes
7 David 2 CityD Yes
8 Maria 3 CityA Yes
9 Joan 3 CityB No
10 Pau 3 CityD Yes
11 Montse 3 CityC No
25
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection

Graph visualization: 

Same color – same shift Same color – same city

26
(*) Edge weights: Same shift (+1), same city (+1), if same shift and same city, public transportation (+1)
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection

1 1

1 3 1
1
1

3 1
1
3

1
1

1
3
1
1
1
1
1 1
1 1 1

2
27
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection – solution (Maximal cliques)

['Jaume', 'Raquel', 'Maria’]  ['Jaume', 'Raquel', 'Cesc', 'Anna’]   
['Cesc', 'Montse', 'Anna’]  ['Eva', 'Joan’]  ['Eva', 'Cristina', 'Marta', 'David’]  
['Pau', 'Maria', 'Joan', 'Montse’]  ['Pau', 'Cristina', 'Marta', 'David'] 

28
Graphs and Network Analysis Artificial Intelligence Degree ‐ UAB

Community detection – solution (Girvan Newman)


Level 0: [{'Jaume'}, {'Raquel'}, {'Cesc'}, {'Anna'}, {'Maria'}, {'Montse'}, {'Cristina'}, {'Marta'}, {'Eva'}, {'David'}, {'Pau'}, {'Joan'}] 
Level 1: [{'Jaume'}, {'Raquel'}, {'Cesc'}, {'Anna'}, {'Maria'}, {'Montse'}, {'Cristina'}, {'Marta'}, {'Eva'}, {'David', 'Pau'}, {'Joan'}] 
Level 2: [{'Jaume'}, {'Raquel'}, {'Cesc'}, {'Anna'}, {'Maria'}, {'Montse'}, {'Cristina'}, {'David', 'Marta', 'Pau'}, {'Eva'}, {'Joan'}] 
Level 3: [{'Jaume'}, {'Raquel'}, {'Cesc'}, {'Anna'}, {'Maria'}, {'Montse'}, {'David', 'Marta', 'Cristina', 'Pau'}, {'Eva'}, {'Joan'}]
Level 4: [{'Jaume'}, {'Raquel'}, {'Cesc', 'Anna'}, {'Maria'}, {'Montse'}, {'David', 'Marta', 'Cristina', 'Pau'}, {'Eva'}, {'Joan'}]
Level 5: [{'Jaume'}, {'Cesc', 'Anna', 'Raquel'}, {'Maria'}, {'Montse'}, {'David', 'Marta', 'Cristina', 'Pau'}, {'Eva'}, {'Joan'}] 
Level 6: [{'Jaume', 'Anna', 'Raquel', 'Cesc'}, {'Maria'}, {'Montse'}, {'David', 'Marta', 'Cristina', 'Pau'}, {'Eva'}, {'Joan'}] 
Level 7: [{'Jaume', 'Anna', 'Raquel', 'Cesc'}, {'Maria'}, {'Montse'}, {'Cristina', 'Marta', 'David', 'Pau', 'Eva'}, {'Joan'}] 
Level 8: [{'Anna', 'Jaume', 'Raquel', 'Montse', 'Cesc'}, {'Maria'}, {'Cristina', 'Marta', 'David', 'Pau', 'Eva'}, {'Joan'}] 
Level 9: [{'Anna', 'Jaume', 'Raquel', 'Maria', 'Montse', 'Cesc'}, {'Cristina', 'Marta', 'David', 'Pau', 'Eva'}, {'Joan'}] 
Level 10: [{'Anna', 'Jaume', 'Raquel', 'Maria', 'Montse', 'Cesc'}, {'Cristina', 'Marta', 'Joan', 'David', 'Pau', 'Eva'}]

29

You might also like