English Version:
- Overview
- Algorithms
- Project Structure
- Installation
- Usage
- Input Format
- Output Format
- Algorithm Details
نسخه فارسی:
This project implements two approaches to the Graph Coloring Problem:
- Sequential Algorithm: Traditional greedy coloring approach
- Parallel Algorithm: Using NetworkX library with optimized strategy
The Graph Coloring Problem is a classic NP-complete problem where the goal is to assign colors to vertices of a graph such that no two adjacent vertices share the same color, using the minimum number of colors possible.
- File:
sequential.py - Strategy: Greedy approach processing nodes in sorted order
- Time Complexity: O(V + E) where V is vertices and E is edges
- Approach: Colors each node with the smallest available color that doesn't conflict with neighbors
- File:
parallel.py - Library: NetworkX (with greedy_color using "largest_first" strategy)
- Strategy: Processes nodes in order of degree (largest first)
- Advantage: Often produces better results in fewer colors
graph-coloring/
│
├── input.txt # Sample input file with edges
├── output_sequential.txt # Output from sequential algorithm
├── output_parallel.txt # Output from parallel algorithm
├── sequential.py # Sequential implementation
├── parallel.py # Parallel implementation (using NetworkX)
└── README.md # This file
python >= 3.7
networkx >= 2.5# Install required packages
pip install networkxpython sequential.pypython parallel.pyBoth scripts read from input.txt and save results to their respective output files.
The input file should contain edges, one per line, in the format:
node1-node2
Example (input.txt):
1-2
2-3
4-5
5-6
3-4
2-4
1-5
The output files contain:
- First line: Total number of colors used
- Second line: Color assignment for each node (space-separated)
Example (output_sequential.txt):
Number of color 3
Node color 1 2 1 2 3 2
The sequential algorithm works as follows:
-
Initialization: Create an empty dictionary to store node colors
-
Helper Functions:
get_neighbors(node): Returns all neighbors of a given nodeget_available_color(node): Finds the smallest color not used by any neighbor
-
Main Process:
- Sort all nodes
- For each node in order:
- Get colors used by its neighbors
- Assign the smallest available color (starting from 1)
- Count total colors and return color assignments
-
Time Complexity Analysis:
- Sorting nodes: O(V log V) where V = number of vertices
- Main loop: O(V) iterations
- For each node:
get_neighbors(): O(E) - scans all edges in worst caseget_available_color(): O(degree of node) - at most O(V)
- Total: O(V × E) in worst case, O(V²) for dense graphs
- Space Complexity: O(V) for storing colors dictionary
-
Why This Works:
- By processing in sorted order, we ensure consistency
- Greedy approach guarantees a valid coloring (not necessarily optimal)
- Practical efficiency despite theoretical complexity due to sparse graphs
The parallel algorithm leverages NetworkX library:
-
Graph Construction: Build a NetworkX Graph from edges
- Time: O(E) where E = number of edges
-
Coloring Strategy - "largest_first":
- Processes nodes in descending order of their degree (number of connections)
- Rationale: Nodes with more connections have fewer color options, so color them first
- This strategy often uses fewer colors than random ordering
-
Color Assignment:
- Returns a dictionary mapping each node to its color
- Extract total color count and create ordered list
-
Time Complexity Analysis:
- Graph construction: O(E)
- Sorting by degree: O(V log V)
- Coloring process: O(V + E) - visits each vertex and edge once
- Total: O(V log V + E) - significantly better than sequential in most cases
- Space Complexity: O(V + E) for graph storage
-
Why This Works:
- More efficient for complex graphs
- The "largest_first" heuristic generally produces better solutions
- Leverages well-tested library code optimized for performance# Graph Coloring Project
این پروژه دو روش برای حل مسئله رنگامیزی گراف را پیادهسازی میکند:
- الگوریتم ترتیبی: روش سبزچین سنتی
- الگوریتم موازی: استفاده از کتابخانه NetworkX
مسئله رنگامیزی گراف یک مسئله کلاسیک NP-complete است که هدف آن تخصیص رنگها به رئوس گراف بهگونهای است که هیچ دو راس مجاور رنگ یکسانی نداشته باشند و از کمترین تعداد رنگ استفاده شود.
- فایل:
sequential.py - روش: رویکرد سبزچین با پردازش گرهها به ترتیب مرتب
- پیچیدگی زمانی: O(V + E) که V تعداد رئوس و E تعداد یالها است
- نحوه کار: هر گره را با کوچکترین رنگ موجود که با همسایگان تضاد ندارد رنگ میکند
- فایل:
parallel.py - کتابخانه: NetworkX (با greedy_color و استراتژی "largest_first")
- روش: پردازش گرهها بر اساس درجه (بزرگترین اول)
- مزیت: معمولاً از رنگهای کمتری استفاده میکند
graph-coloring/
│
├── input.txt # فایل نمونه حاوی یالها
├── output_sequential.txt # خروجی الگوریتم ترتیبی
├── output_parallel.txt # خروجی الگوریتم موازی
├── sequential.py # پیادهسازی ترتیبی
├── parallel.py # پیادهسازی موازی (با NetworkX)
└── README.md # این فایل
python >= 3.7
networkx >= 2.5# نصب بستههای مورد نیاز
pip install networkxpython sequential.pypython parallel.pyهر دو اسکریپت از فایل input.txt میخوانند و نتایج را در فایلهای خروجی مربوطه ذخیره میکنند.
فایل ورودی باید حاوی یالها باشد، هر یال در یک خط بهصورت زیر:
node1-node2
مثال (input.txt):
1-2
2-3
4-5
5-6
3-4
2-4
1-5
فایلهای خروجی حاوی موارد زیر هستند:
- خط اول: تعداد کل رنگهای استفادهشده
- خط دوم: تخصیص رنگ برای هر گره (جداشده با فاصله)
مثال (output_sequential.txt):
Number of color 3
Node color 1 2 1 2 3 2
الگوریتم ترتیبی بهصورت زیر کار میکند:
-
مقداردهی اولیه: یک دیکشنری خالی برای ذخیره رنگهای گرهها ایجاد کنید
-
توابع کمکی:
get_neighbors(node): تمام همسایگان یک گره را برمیگرداندget_available_color(node): کوچکترین رنگی را پیدا میکند که توسط هیچ همسایه استفاده نشدهاست
-
فرایند اصلی:
- تمام گرهها را مرتب کنید
- برای هر گره بهترتیب:
- رنگهای استفادهشده توسط همسایگان را بدست آورید
- کوچکترین رنگ موجود (شروع از 1) را تخصیص دهید
- تعداد کل رنگها و تخصیص رنگها را برگردانید
-
تحلیل پیچیدگی زمانی:
- مرتبسازی گرهها: O(V log V) که V = تعداد رئوس
- حلقه اصلی: O(V) تکرار
- برای هر گره:
get_neighbors(): O(E) - در بدترین حالت تمام یالها را اسکن میکندget_available_color(): O(درجه گره) - حداکثر O(V)
- کل: O(V × E) در بدترین حالت، O(V²) برای گرافهای متراکم
- پیچیدگی فضایی: O(V) برای ذخیره دیکشنری رنگها
-
چرا این روش کار میکند:
- با پردازش بهترتیب، ثبات را تضمین میکنیم
- رویکرد سبزچین یک رنگامیزی معتبر را تضمین میکند (نهلزوماً بهینه)
- کارایی عملی علیرغم پیچیدگی تئوریای به دلیل گرافهای کمتراکم
الگوریتم موازی از کتابخانه NetworkX استفاده میکند:
-
ساخت گراف: یک گراف NetworkX از یالها بسازید
- زمان: O(E) که E = تعداد یالها
-
استراتژی رنگامیزی - "largest_first":
- گرهها را به ترتیب نزولی درجهشان پردازش میکند (تعداد اتصالات)
- منطق: گرههای با اتصالات بیشتر گزینههای رنگی کمتری دارند، بنابراین ابتدا آنها را رنگ کنید
- این استراتژی معمولاً رنگهای کمتری نسبت به ترتیب تصادفی استفاده میکند
-
تخصیص رنگ:
- یک دیکشنری برمیگرداند که هر گره را به رنگاش نگاشت میکند
- تعداد رنگ کل و یک لیست مرتبشده را استخراج کنید
-
تحلیل پیچیدگی زمانی:
- ساخت گراف: O(E)
- مرتبسازی بر اساس درجه: O(V log V)
- فرایند رنگامیزی: O(V + E) - هر راس و یال را یکبار بازدید میکند
- کل: O(V log V + E) - بسیار بهتر از الگوریتم ترتیبی در اکثر موارد
- پیچیدگی فضایی: O(V + E) برای ذخیرهسازی گراف
-
چرا این روش کار میکند:
- برای گرافهای پیچیده کارایی بیشتر
- الگوریتم ابتکاری "largest_first" معمولاً راهحلهای بهتری تولید میکند
- از کد کتابخانه آزمایششده استفاده میکند که برای عملکرد بهینه شدهاست
Open Source