What is Affinity Propagation Algorithm?
Affinity Propagation (AP) is an advanced clustering algorithm used in machine learning and data mining to group similar data points into clusters. Unlike traditional clustering methods such as K-Means, which require the number of clusters to be specified beforehand, Affinity Propagation automatically determines the optimal number of clusters based on the relationships among data points. The algorithm identifies representative data points called exemplars, which serve as cluster centers. Instead of randomly selecting cluster centroids, Affinity Propagation exchanges messages between data points to determine which points are most suitable to act as exemplars. This message-passing approach allows the algorithm to discover clusters naturally from the data. Affinity Propagation was introduced by Brendan J. Frey and Delbert Dueck in 2007 and has become popular in applications involving pattern recognition, image processing, recommendation systems, and bioinformatics.
Introduction to Affinity Propagation Algorithm
Clustering is one of the most important unsupervised learning techniques used to discover hidden patterns in datasets. Traditional clustering algorithms often require prior knowledge of the number of clusters, which may not always be available. Additionally, algorithms such as K-Means can be sensitive to initialization and may converge to local optima. Affinity Propagation addresses these limitations through a unique approach based on message passing. Rather than assigning points to predefined cluster centers, every data point is initially considered a potential exemplar. Data points then exchange information about how appropriate it would be for one point to serve as the exemplar for another. The algorithm works by iteratively updating two types of messages:
- Responsibility (r) – Sent from a data point to a candidate exemplar.
- Availability (a) – Sent from a candidate exemplar to a data point.
Through repeated message exchanges, the algorithm identifies a set of exemplars and assigns each data point to its nearest exemplar, thereby forming clusters. Key characteristics of Affinity Propagation include:
- No need to specify the number of clusters beforehand.
- Identifies actual data points as cluster centers.
- Handles complex cluster structures.
- Often achieves better clustering quality than centroid-based methods.
- Suitable for medium-sized datasets.
Detailed Affinity Propagation Algorithm
Step 1: Prepare the Similarity Matrix
The first step is to calculate similarities between all pairs of data points.
Let:

represent the similarity between point iii and point k.
A commonly used similarity measure is the negative squared Euclidean distance:

where:
- xi = data point i
- xk = data point k
Higher similarity values indicate greater closeness.
The similarity matrix is:

Step 2: Set Preference Values
Each point has a preference value that indicates how likely it is to become an exemplar.

where:
- p = preference value
Larger preference values produce more clusters, while smaller values produce fewer clusters.
A common choice is:

Step 3: Initialize Messages
Initialize responsibilities and availabilities:

for all data point pairs.
Step 4: Compute Responsibility Messages
Responsibility measures how suitable candidate exemplar kkk is for data point iii.
Formula:

Interpretation:
- Positive responsibility indicates candidate k is a strong exemplar.
- Negative responsibility indicates other candidates are better.
Step 5: Compute Availability Messages
Availability measures how appropriate it is for point iii to choose k as its exemplar.
For i≠k:

For self-availability:

Interpretation:
- High availability suggests strong support for exemplar k.
- Negative values indicate weak support.
Step 6: Apply Damping
To avoid oscillations during updates, damping is used.

where:

Typically:

Step 7: Update Messages Iteratively
Repeat:
- Responsibility updates
- Availability updates
- Damping
until convergence or a maximum number of iterations is reached.
Step 8: Identify Exemplars
Compute:

If:

then point k becomes an exemplar.
Step 9: Assign Data Points to Clusters
Each point is assigned to the exemplar that maximizes:

Thus:

Step 10: Generate Final Clusters
The resulting exemplars become cluster centers, and each data point belongs to the cluster represented by its selected exemplar.

Affinity Propagation begins by treating every data point as a potential cluster center. Unlike K-Means, where centroids are initialized randomly, Affinity Propagation allows the data itself to determine which points should become exemplars. The process starts with calculating similarities between all pairs of points. Similarity values indicate how well one point represents another. After constructing the similarity matrix, preference values are assigned to every point. These preference values influence the likelihood of points becoming exemplars. If the preference values are increased, more clusters tend to emerge. Conversely, lower preference values generally lead to fewer clusters.
Initially, all responsibility and availability messages are set to zero. The algorithm then enters an iterative message-passing phase. During responsibility updates, each point evaluates all possible exemplars and sends a message indicating which candidate best represents it. The responsibility value compares one candidate against all competing candidates. This creates a competitive environment where only strong exemplar candidates receive positive support.
Next, availability messages are computed. Availability reflects the collective support that a candidate exemplar receives from neighboring points. If many points strongly prefer a candidate exemplar, its availability increases. Availability messages therefore aggregate evidence from multiple data points and reinforce promising exemplars.
To ensure numerical stability, damping is applied to message updates. Damping prevents rapid fluctuations and oscillations in the responsibility and availability values. By blending new values with previous values, the algorithm converges more smoothly. The responsibility and availability updates continue iteratively until convergence. Convergence occurs when the set of exemplars remains unchanged over several iterations. At this stage, the algorithm identifies points whose combined responsibility and availability values are positive. These points become exemplars. Finally, every data point is assigned to the exemplar with the highest combined responsibility and availability score. The result is a set of clusters where each cluster is represented by a real data point rather than an artificial centroid. This property makes Affinity Propagation particularly useful for applications requiring interpretable cluster representatives.
Example of How Affinity Propagation Works
Consider six customers represented by their purchase behavior:
| Customer | Spending Score |
| A | 10 |
| B | 12 |
| C | 15 |
| D | 80 |
| E | 85 |
| F | 90 |
The objective is to group similar customers.
Step 1: Calculate Similarities
The algorithm computes pairwise similarities based on distance.
Customers A, B, and C have high similarity because their spending values are close.
Customers D, E, and F also have high similarity.
Step 2: Initialize Messages
Responsibilities:

Availabilities:

Step 3: Message Passing
After several iterations:
- Customer B emerges as an exemplar for A, B, and C.
- Customer E emerges as an exemplar for D, E, and F.
Step 4: Final Clusters
Cluster 1
- A
- B (Exemplar)
- C
Cluster 2
- D
- E (Exemplar)
- F
The algorithm automatically discovers two clusters without being explicitly told the number of clusters.
Advantages and Disadvantages of Affinity Propagation Algorithm
Advantages
- No Need to Specify Number of Clusters: One of the most significant advantages of Affinity Propagation is that it automatically determines the appropriate number of clusters from the dataset. Traditional clustering algorithms such as K-Means require users to specify the number of clusters before the clustering process begins, which can be difficult when the data structure is unknown. Affinity Propagation eliminates this limitation by identifying exemplars through message passing, allowing clusters to emerge naturally. This makes the algorithm particularly useful in exploratory data analysis where the optimal number of groups is not known in advance.
- Finds Real Data Representatives: Affinity Propagation selects actual data points as cluster centers, known as exemplars. Unlike algorithms that calculate artificial centroids, the exemplars correspond to real observations from the dataset. This improves the interpretability of clustering results because cluster representatives can be directly examined and understood. In applications such as customer segmentation, document clustering, and image analysis, having real data points represent clusters makes the outcomes more meaningful and actionable.
- Handles Complex Data Structures: The algorithm can effectively identify clusters with varying sizes, densities, and shapes. Many clustering techniques perform best when clusters are spherical and evenly distributed, but real-world data often contains irregular patterns. Affinity Propagation evaluates pairwise similarities among all data points, allowing it to discover complex relationships and form clusters that better reflect the underlying data structure. This flexibility makes it suitable for diverse datasets encountered in machine learning and data mining applications.
- Better Initialization Strategy: Unlike K-Means, which relies on randomly selected initial centroids, Affinity Propagation treats every data point as a potential cluster center at the start of the algorithm. Since there is no random initialization, the clustering results are generally more stable and reproducible. This reduces the possibility of obtaining different results from multiple executions and eliminates the need to run the algorithm repeatedly to find a good initialization.
- High Clustering Accuracy: Affinity Propagation often produces highly accurate clustering results because it considers similarities between all pairs of data points. The responsibility and availability messages exchanged during the iterative process allow the algorithm to carefully evaluate potential exemplars before forming clusters. As a result, it can uncover meaningful patterns and groupings that may be missed by simpler clustering methods. In many practical applications, Affinity Propagation provides better cluster quality than traditional centroid-based algorithms.
- Robust to Noise: Real-world datasets frequently contain noisy, incomplete, or irrelevant observations. Affinity Propagation is relatively robust to such noise because the message-passing mechanism focuses on identifying the most representative exemplars. Data points that do not strongly support any exemplar tend to have less influence on the final clustering outcome. This ability to reduce the impact of noisy observations improves the reliability and stability of the generated clusters.
- Supports Various Similarity Measures: The algorithm is highly flexible because it can work with different similarity measures depending on the nature of the data. While negative squared Euclidean distance is commonly used, other similarity metrics such as cosine similarity, Manhattan distance, correlation coefficients, or custom domain-specific measures can also be applied. This adaptability enables Affinity Propagation to be used across a wide range of applications, including text mining, recommendation systems, image processing, and bioinformatics.
Disadvantages
- High Computational Complexity: A major drawback of Affinity Propagation is its computational cost. The algorithm requires calculating and storing similarities between every pair of data points, resulting in a computational complexity of:O(N2) As the number of data points increases, the required computations grow rapidly. For large datasets, this can lead to significant processing time and resource consumption, making the algorithm less efficient than some alternative clustering techniques.
- Poor Scalability: Because of its quadratic complexity, Affinity Propagation does not scale well to very large datasets. Datasets containing hundreds of thousands or millions of observations may require excessive computation and memory resources. In such situations, clustering algorithms designed specifically for large-scale data, such as Mini-Batch K-Means or distributed clustering approaches, may be more practical.
- Sensitive to Preference Values: The preference parameter determines how likely a data point is to become an exemplar. Small changes in preference values can significantly affect the number of clusters generated. If the preference value is set too high, the algorithm may create too many clusters. If it is set too low, important clusters may be merged. Consequently, selecting an appropriate preference value often requires experimentation and domain expertise.
- Longer Runtime: Affinity Propagation relies on multiple rounds of iterative message passing between data points. During each iteration, responsibility and availability values must be updated and recalculated. Depending on the dataset size and convergence criteria, the algorithm may require many iterations before reaching a stable solution. As a result, its execution time is often longer than that of simpler clustering methods such as K-Means.
- Convergence Issues: Although Affinity Propagation generally converges to a stable solution, convergence is not always guaranteed. In some cases, the responsibility and availability messages may oscillate, preventing the algorithm from stabilizing. To address this issue, damping factors are introduced to smooth message updates. However, selecting inappropriate damping values can still lead to slow convergence or unstable clustering results.
- Memory Intensive: The algorithm requires storing the complete similarity matrix as well as responsibility and availability matrices. For a dataset containing thousands of observations, these matrices can become extremely large. Consequently, Affinity Propagation can consume substantial amounts of memory, particularly when working with high-dimensional data. Memory limitations often become a practical constraint when applying the algorithm to large-scale problems.
- Difficult Parameter Tuning: Although Affinity Propagation eliminates the need to specify the number of clusters directly, it introduces other parameters such as preference values and damping factors. The quality of clustering results can be highly dependent on these parameters. Determining suitable values often involves trial-and-error experimentation, cross-validation, or domain-specific knowledge. This parameter sensitivity can increase the complexity of implementing and optimizing the algorithm in real-world applications.
Applications of Affinity Propagation Algorithm
- Image Segmentation: Affinity Propagation is widely used in image segmentation to divide an image into meaningful regions based on pixel similarity. The algorithm groups pixels that share similar characteristics such as color, texture, intensity, or spatial location. By identifying representative pixels, or exemplars, it can effectively separate objects from the background and distinguish different regions within an image. This application is particularly useful in medical imaging, satellite image analysis, object detection, and facial recognition systems.
- Computer Vision: In computer vision, Affinity Propagation helps identify patterns and relationships among visual features extracted from images and videos. The algorithm can cluster similar visual descriptors, enabling tasks such as object recognition, scene classification, image categorization, and feature matching. Since it automatically determines the number of clusters, it is highly effective in organizing large collections of visual data without requiring prior knowledge of the dataset structure.
- Document Clustering: Document clustering is one of the most common applications of Affinity Propagation in text mining and natural language processing. The algorithm groups documents with similar content, topics, or keywords into clusters. By identifying representative documents as exemplars, it simplifies information retrieval and knowledge discovery. Search engines, digital libraries, and content management systems use document clustering to organize large collections of articles, research papers, and web pages efficiently.
- Recommendation Systems: Recommendation systems use Affinity Propagation to identify groups of users with similar preferences, behaviors, or purchasing patterns. By clustering customers based on historical interactions, businesses can recommend products, services, or content that are likely to interest specific user groups. This approach improves recommendation accuracy and enhances customer satisfaction. Online platforms such as streaming services, e-commerce websites, and social media applications frequently benefit from clustering-based recommendation techniques.
- Bioinformatics: Bioinformatics involves analyzing complex biological data, and Affinity Propagation is widely used for clustering gene expression profiles, DNA sequences, protein structures, and biological pathways. The algorithm helps researchers identify genes with similar functions, discover disease-related patterns, and classify biological samples. Because it can automatically determine the number of clusters, it is particularly valuable in exploratory biological research where the underlying group structure is often unknown.
- Social Network Analysis: Social networks generate massive amounts of interaction data among users. Affinity Propagation helps identify communities by clustering users who share similar behaviors, interests, friendships, or communication patterns. These communities can reveal hidden social structures and influential individuals within a network. Businesses and researchers use this information to understand user engagement, detect trends, improve targeted advertising, and analyze information diffusion across social platforms.
- Market Segmentation: Businesses rely on market segmentation to understand diverse customer groups and develop effective marketing strategies. Affinity Propagation clusters customers based on demographic characteristics, purchasing behavior, preferences, and spending habits. Each cluster represents a distinct customer segment with similar needs and interests. Organizations can use these insights to create personalized marketing campaigns, improve customer retention, and optimize product offerings for specific target audiences.
- Fraud Detection: Financial institutions and cybersecurity systems use Affinity Propagation to detect fraudulent activities. The algorithm clusters transactions, user behaviors, or network activities based on similarity patterns. Transactions that significantly differ from normal behavior or belong to suspicious clusters can be flagged for further investigation. This capability helps organizations identify credit card fraud, insurance fraud, money laundering activities, and cybersecurity threats more effectively.
- Healthcare Analytics: In healthcare, Affinity Propagation assists in analyzing patient records, medical histories, laboratory results, and diagnostic information. By clustering patients with similar symptoms or disease characteristics, healthcare providers can identify disease patterns, treatment responses, and risk factors. The algorithm supports personalized medicine, clinical decision-making, and medical research by helping healthcare professionals better understand patient populations and improve treatment outcomes.
- Sensor Networks: Wireless sensor networks often consist of hundreds or thousands of interconnected sensor nodes. Affinity Propagation can cluster these sensors into efficient communication groups based on factors such as location, energy levels, and data similarity. Organizing sensors into clusters reduces communication overhead, conserves energy, and improves network performance. This application is particularly important in environmental monitoring, smart cities, industrial automation, and Internet of Things (IoT) systems.
- E-Commerce Analytics: E-commerce companies use Affinity Propagation to analyze customer purchasing behavior and shopping preferences. The algorithm clusters customers with similar browsing histories, buying patterns, and product interests. These insights enable businesses to personalize recommendations, optimize inventory management, design targeted promotions, and improve customer experiences. Clustering also helps identify valuable customer segments that contribute significantly to business revenue.
Conclusion
Affinity Propagation is a powerful clustering algorithm that offers a unique alternative to traditional methods such as K-Means and Hierarchical Clustering. By treating every data point as a potential exemplar and using an iterative message-passing mechanism, the algorithm automatically discovers meaningful clusters without requiring the number of clusters to be specified in advance. The core concepts of similarity, responsibility, and availability enable Affinity Propagation to identify representative exemplars that serve as cluster centers. Its ability to use real data points as exemplars improves interpretability and often results in higher-quality clustering outcomes. Although the algorithm can be computationally intensive for very large datasets, it performs exceptionally well on medium-sized datasets where accurate cluster discovery is essential. Today, Affinity Propagation is widely applied in image processing, recommendation systems, bioinformatics, document analysis, healthcare, social network analysis, and numerous other domains. Its flexibility, robustness, and automatic cluster discovery capabilities make it an important algorithm in the field of unsupervised machine learning and data mining.
Frequently Asked Questions (FAQs)
What is the main purpose of the Affinity Propagation Algorithm?
The main purpose of Affinity Propagation is to cluster similar data points while automatically determining the number of clusters and selecting representative exemplars.
How is Affinity Propagation different from K-Means?
K-Means requires the number of clusters to be specified in advance and uses artificial centroids, whereas Affinity Propagation automatically determines clusters and uses actual data points as exemplars.
What are responsibility and availability in Affinity Propagation?
Responsibility measures how suitable a candidate exemplar is for a data point, while availability measures how strongly a candidate exemplar is supported by other data points.
Why are preference values important in Affinity Propagation?
Preference values influence how many exemplars are selected. Higher preference values generally produce more clusters, while lower values produce fewer clusters.
What are the major applications of Affinity Propagation?
Major applications include image segmentation, document clustering, recommendation systems, bioinformatics, healthcare analytics, social network analysis, market segmentation, and fraud detection.