1 of 58

Graphs (Part 02)

Introduction to Graph Theory

Fardina Fathmiul Alam

​

CMSC 320 - Introduction to Data Science

​

Community Detection & The Girvan-Newman algorithm

2 of 58

TODAY WE WILL KNOW FIRST

  • What is “Community”
  • How to remove/ eliminate edges that have the highest number of shortest paths - “Edge Betweenness”

​

Before discussing about The Girvan-Newman algorithm.

​

​

GIRVAN-NEWMAN ALGORITHM

​

The Girvan-Newman algorithm for the detection and analysis of community structure relies on the iterative elimination of edges that have the highest number of shortest paths between nodes passing through them. By removing edges from the graph one-by-one, the network breaks down into smaller pieces, so-called communities. The algorithm was introduced by Michelle Girvan and Mark Newman.

3 of 58

WHAT IS COMMUNITY?

Real-life graphs are not random

  • E.g., in a social network people pick their friends based on their common interests and activities.

​

We expect that the nodes in a graph will be organized in communities.

  • Groups of vertices which probably share common properties and/or play similar roles within the graph

​

How do we find them?

  • A subset of nodes in communities will be densely connected to each other, and sparsely (loosely) connected to the nodes in other communities in the same graph.
  • Sounds familiar?

A group of nodes that are:

  • Densely connected internally
  • Sparsely connected externally

4 of 58

PROBLEM: CAN WE IDENTIFY GROUPS OF DENSELY CONNECTED NODES?

(Adapted from: Mining of Massive Datasets, http://www.mmds.org

WHY COMMUNITIES MATTER?

​

5 of 58

COMMUNITIES: FOOTBALL CONFERENCES

Nodes: Football Teams,

Edges: Matches/ Game played, Communities: Conferences

​

6 of 58

WHY COMMUNITIES MATTER?

​

7 of 58

COMMUNITY DETECTION

7

A co-authorship network of physicists and mathematicians

(Courtesy: Easley & Kleinberg)

How can the tightly clustered groups be identified?

Can we automatically detect groups of tightly connected nodes?

→ Core Problem

8 of 58

COMMUNITY: Key Insight?

Informally: “tightly-knit region” of the network.

Nodes are highly connected within, weakly connected outside

Key Questions:

  • How do we identify such regions?
  • How do we separate them?
  • What defines “tightly-knit”? (varies by context)
  • Can communities be nested?

Insight: Bridges (edges connecting groups) help separate communities

​

8

(Courtesy: Easley & Kleinberg)

An example of a nested structure of the communities

Removal of a bridge separates the graph into disjoint components

bridges

9 of 58

RECAP: CENTRALITY

  • Betweenness: Number of shortest paths through a node or a edge
  • Closeness: Average distance to other nodes
  • Degree: Number of connections to other nodes

10 of 58

EDGE IMPORTANCE

Betweenness of edge (𝑎,𝑏) (𝐵(𝑎,𝑏)): For each pair of nodes 𝑥,𝑦 compute the number of shortest paths that include (𝑎,𝑏)

The edge betweenness centrality (EBC) can be defined as the number of shortest paths that pass through an edge (more specifically passing through the endpoints of the edge) in a network.

  • These edges act like bridges
  • High value → edge connects different groups

11 of 58

12 of 58

Example: Practical Applications of Community Detection

Social Networks: Identifies individuals with similar interests; Helps understand social dynamics and group behavior.

​

Epidemiology: Recognizes clusters to prevent disease spread. Vital for public health strategies.

​

Internet Communities: Groups pages with similar content. Organizes online information effectively. Enables insights into diverse networks.

13 of 58

​

Girvan-Newman Method [Newman and Girvan, 2004] COMMUNITY DETECTION ALGORITHM

​

Steps:

  • Computer the edges of betweenness first.
  • Remove the highest betweenness edge(s).
  • Repeat the same step with the remainder graph.
  • Continue this until the graph breaks down into predefined or data-driven numbers of communities.

​

13

Michelle Girvan

Notes: Girvan-Newman doesn’t give just one answer: it gives a hierarchy of possible communities, and we choose the best level (often using modularity).

14 of 58

A SAMPLE OF HOW HIERARCHICAL RESULTS OF GIRMAN-NEWMAN

Removing high-betweenness edges reveals community structure

​

The graph splits into smaller groups → hierarchical partitioning

This forms a tree called a dendrogram

  • Root: entire graph
  • Leaves: individual nodes

15 of 58

Example: GIRVAN-NEWMAN METHOD

​

16 of 58

Example: GIRVAN-NEWMAN METHOD

​

17 of 58

Example: GIRVAN-NEWMAN METHOD

​

17

Betweenness(7-8)= 7*7 = 49

How?

First, Calculate Edge Betweenness

18 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

Edge Betweenness Example

Calculate total flow over edge 7-8

Edge 7-8 in the graph here carries info from each of the 7 nodes on the left (incl. node 7) and the 7 nodes on the right (incl. node 8) # shortest paths through Edge 7-8 is 7*7 = 49

19 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

One unit flows over 7-8 to get from 1 to 8

20 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

One unit flows over 7-8 to get from 1 to 9

21 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

One unit flows over 7-8 to get from 1 to 10

7

8

3

2

1

11

9

10

6

4

5

13

12

14

22 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

7 total units flow over 7-8 to get from 1 to nodes 8-14

7

8

3

2

1

11

9

10

6

4

5

13

12

14

23 of 58

7 total units flow over 7-8 to get from 2 to nodes 8-14

7

8

3

2

1

11

9

10

6

4

5

13

12

14

24 of 58

7 total units flow over 7-8 to get from 3 to nodes 8-14 and so on..

7

8

3

2

1

11

9

10

6

4

5

13

12

14

25 of 58

7 x 7 = 49 total units flow over 7-8 from nodes 1-7 to 8-14

7

8

3

2

1

11

9

10

6

4

5

13

12

14

26 of 58

Edge betweenness = 49

7

8

3

2

1

11

9

10

6

4

5

13

12

14

27 of 58

Back to: GIRVAN-NEWMAN METHOD

27

Betweenness(7-8)= 7*7 = 49

28 of 58

Example: GIRVAN-NEWMAN METHOD

​

28

Betweenness(7-8)= 7*7 = 49

Betweenness(3-7) = Betweenness(6-7) = Betweenness(8-9) = Betweenness(8-12) = ?

Betweenness(1-3) = ?

Which edge is having highest betweenness?

3*11= 33

1*12 = 12

29 of 58

30 of 58

Example: GIRVAN-NEWMAN METHOD

​

30

Betweenness(1, 3) = 1X5=5

Betweenness(3,7) = Betweenness(6,7) = Betweenness(8,9) = Betweenness(8,12) = 3X4=12

Removed

Next, Remove the edge having highest Edge Betweenness

31 of 58

Example: GIRVAN-NEWMAN METHOD

​

31

Why is Betweenness now??

Betweenness of every edge = 1

Repeat the Process

32 of 58

32

G=nx.Graph()

​

# Returns an iterator over partitions at

# different hierarchy levels

nx.girvan_newman(G)

Example: GIRVAN-NEWMAN METHOD

​

Problem: Over-splitting

  • Algorithm keeps splitting
  • Which result is best?

33 of 58

ALL TOGETHER: GIRVAN-NEWMAN METHOD

33

34 of 58

35 of 58

Solution: Introducing Modularity (An Improvement)

“Modularity” - a helpful metric to assess the strength of the community structure found.

​

  • We can use Modularity to evaluate community structures.

​

​

Modularity can be represented and calculated in different ways depending on the specific community detection algorithm. We will focus on “Modularity Score Q”.

​

​

36 of 58

Modularity

  • Measures how strong a community structure is
  • Compares:
    • Actual edges within communities
    • vs. Expected edges (random network)

Higher modularity → stronger communities� (more internal edges, fewer between groups)

​

37 of 58

An Improvement: Introducing Modularity

After each edge removal, we compute modularity (Q) to evaluate the quality of the split.

​

​

​

  • Higher Q → stronger community structure
  • Q∈[−1,1], values closer to 1 indicate better splits
  • Repeat process and choose the split that maximizes Q

We stop after all splits, but choose the split where modularity (Q) is highest.

38 of 58

39 of 58

A modularity of 0 suggests that this particular division into two communities does not reflect a strong community structure

Final Takaways:

  • Communities = dense groups
  • Betweenness finds bridges
  • Girvan-Newman removes bridges
  • Modularity helps choose best split

40 of 58

Another Example: Strong Community Structure

Graph: Nodes: A, B, C, D, Edges: (A–B), (C–D)� → Two disconnected pairs

Communities:

  • Community 1: {A, B}
  • Community 2: {C, D}

​

Q:0.25> 0 : strong community structure

41 of 58

NETWORKX: VIZ

Can render via Matplotlib or GraphViz

​

​

​

​

​

Many different layout engines, aesthetic options, etc

​

41

import matplotlib.pyplot as plt

​

G=nx.Graph()

nx.draw(G, with_labels=True)

​

# Save to a PDF

plt.savefig(“my_filename.pdf”)

42 of 58

NETWORKX: VIZ

42

# Cycle with 24 vertices

G=nx.cycle_graph(24)

​

# Compute force-based layout

pos=nx.spring_layout(G,

iterations=200)

​

# Draw the graph

nx.draw(G,pos,

node_color=range(24),

node_size=800,

cmap=plt.cm.Blues)

​

# Save as PNG, then display

plt.savefig(”graph.png")

plt.show()

43 of 58

NETWORKX: VIZ

43

# Branch factor 3, depth 5

G = nx.balanced_tree(3, 5)

​

# Circular layout

pos = graphviz_layout(G,

prog='twopi', args='')

​

# Draw 8x8 figure

plt.figure(figsize=(8, 8))

nx.draw(G, pos,

node_size=20,

alpha=0.5,

node_color="blue",

with_labels=False)

​

plt.axis('equal')

plt.show()

44 of 58

Additional Reading Slides

45 of 58

The Girvan-Newman method for graph partition [Newman and Girvan, 2004]

Step 1: Identify and remove the edge or edges with the highest betweenness centrality from the graph. This might cause the graph to split into separate parts, marking the initial level of regions in the graph's partitioning.

​

Step 2: Recalculate betweenness centrality for all edges and remove the edge or edges with the highest betweenness. This might further break down existing components into smaller ones, revealing nested regions within larger ones.

​

Step 3: Repeat this process iteratively until no edges remain in the graph. At each step, recalculate betweenness centrality for all edges and remove the edge or edges with the highest betweenness. This progressively reveals the hierarchical structure of regions within the graph.

46 of 58

47 of 58

7 x 7 = 49 total units flow over 7-8 from nodes 1-7 to 8-14

7

8

3

2

1

11

9

10

6

4

5

13

12

14

48 of 58

Edge betweenness = 49

7

8

3

2

1

11

9

10

6

4

5

13

12

14

GIRVAN-NEWMAN METHOD

49 of 58

Calculate betweenness for edge 3-7

7

8

3

2

1

11

9

10

6

4

5

13

12

14

50 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

3 units flow from �1-3 to each 4-14 node,

so total = �3 x 11 = 33

51 of 58

Betweenness = 33

for each �symmetric edge

​

33

33

33

33

7

8

3

2

1

11

9

10

6

4

5

13

12

14

52 of 58

Calculate betweenness for edge 1-3

7

8

3

2

1

11

9

10

6

4

5

13

12

14

53 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

Carries all flow to node 1 except from node 2,

so betweenness = 12

54 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

betweenness = 12

for each �symmetric edge

12

12

12

12

12

12

12

12

7

8

3

2

1

11

9

10

6

4

5

13

12

14

55 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

Calculate betweenness for edge 1-2

7

8

3

2

1

11

9

10

6

4

5

13

12

14

56 of 58

3

2

1

Only carries flow �from 1 to 2, so �betweenness = 1

7

8

3

2

1

11

9

10

6

4

5

13

12

14

57 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

betweenness = 1�for each symmetric edge

1

1

1

1

7

8

3

2

1

11

9

10

6

4

5

13

12

14

58 of 58

7

8

3

2

1

11

9

10

6

4

5

13

12

14

Edge with highest betweenness

1

1

1

1

12

12

12

12

12

12

12

12

33

33

33

33

49

7

8

3

2

1

11

9

10

6

4

5

13

12

14