-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rostam/master
Merging from rostam
- Loading branch information
Showing
45 changed files
with
726 additions
and
65,250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/graphtea/extensions/reports/GreedyColoring.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// GraphTea Project: http://github.com/graphtheorysoftware/GraphTea | ||
// Copyright (C) 2012 Graph Theory Software Foundation: http://GraphTheorySoftware.com | ||
// Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology | ||
// Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ | ||
|
||
package graphtea.extensions.reports; | ||
|
||
import graphtea.graph.graph.GraphModel; | ||
import graphtea.graph.graph.RenderTable; | ||
import graphtea.graph.graph.Vertex; | ||
import graphtea.plugins.reports.extension.GraphReportExtension; | ||
import org.codehaus.jettison.json.JSONArray; | ||
import org.codehaus.jettison.json.JSONException; | ||
import org.codehaus.jettison.json.JSONObject; | ||
|
||
import java.util.Iterator; | ||
import java.util.Vector; | ||
|
||
/** | ||
* @author Azin Azadi | ||
*/ | ||
public class GreedyColoring implements GraphReportExtension { | ||
|
||
public String getName() { | ||
return "Greedy Coloring"; | ||
} | ||
|
||
public String getDescription() { | ||
return "The coloring of graph computed by greedy algorithm"; | ||
} | ||
|
||
public Object calculate(GraphModel g) { | ||
int numOfVertices = g.numOfVertices(); | ||
int mapToColors[] = new int[numOfVertices]; | ||
boolean available[] = new boolean[numOfVertices]; | ||
mapToColors[0] = 0; | ||
|
||
for (int u = 1; u < numOfVertices; u++) | ||
mapToColors[u] = -1; | ||
|
||
for (int cr = 0; cr < numOfVertices; cr++) | ||
available[cr] = false; | ||
|
||
for (int u = 1; u < numOfVertices; u++) { | ||
|
||
Iterator<Vertex> it = g.directNeighbors(g.getVertex(u)).iterator(); | ||
while (it.hasNext()) { | ||
int i = it.next().getId(); | ||
if (mapToColors[i] != -1) | ||
available[mapToColors[i]] = true; | ||
} | ||
|
||
for (int color = 0; color < numOfVertices; color++) | ||
if (available[color] == false) { | ||
mapToColors[u] = color; | ||
break; | ||
} | ||
|
||
|
||
it = g.directNeighbors(g.getVertex(u)).iterator(); | ||
while (it.hasNext()) { | ||
int i = it.next().getId(); | ||
if (mapToColors[i] != -1) | ||
available[mapToColors[i]] = false; | ||
} | ||
} | ||
|
||
int max = 0; | ||
for (int i : mapToColors) { | ||
if (max < i) max = i; | ||
} | ||
max++; | ||
|
||
JSONObject jsonObject = new JSONObject(); | ||
try { | ||
jsonObject.put("num_of_colors", max); | ||
JSONArray jsonArray = new JSONArray(); | ||
for (int i = 0; i < mapToColors.length; i++) jsonArray.put(mapToColors[i]); | ||
jsonObject.put("colors", jsonArray); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return jsonObject; | ||
} | ||
|
||
@Override | ||
public String getCategory() { | ||
return "Coloring"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.