// A Java program to implement greedy algorithm for graph coloring import java.io.*; import java.util.*; import java.util.LinkedList;
// This class represents an undirected graph using adjacency list class Graph { private int V; // No. of vertices private LinkedList<Integer> adj[]; //Adjacency List
//Constructor Graph(int v) { V = v; adj = new LinkedList[v]; for (int i=0; i<v; ++i) adj[i] = new LinkedList(); }
//Function to add an edge into the graph void addEdge(int v,int w) { adj[v].add(w); adj[w].add(v); //Graph is undirected }
// Assigns colors (starting from 0) to all vertices and // prints the assignment of colors void greedyColoring() { int result[] = new int[V];
// Initialize all vertices as unassigned Arrays.fill(result, -1);
// Assign the first color to first vertex result[0] = 0;
// A temporary array to store the available colors. False // value of available[cr] would mean that the color cr is // assigned to one of its adjacent vertices boolean available[] = new boolean[V];
// Initially, all colors are available Arrays.fill(available, true);
// Assign colors to remaining V-1 vertices for (int u = 1; u < V; u++) { // Process all adjacent vertices and flag their colors // as unavailable Iterator<Integer> it = adj[u].iterator() ; while (it.hasNext()) { int i = it.next(); if (result[i] != -1) available[result[i]] = false; }
// Find the first available color int cr; for (cr = 0; cr < V; cr++){ if (available[cr]) break; }
result[u] = cr; // Assign the found color
// Reset the values back to true for the next iteration Arrays.fill(available, true); }
// print the result for (int u = 0; u < V; u++) System.out.println("Vertex " + u + " ---> Color " + result[u]); }
// Driver method public static void main(String args[]) { Graph g1 = new Graph(5); g1.addEdge(0, 1); g1.addEdge(0, 2); g1.addEdge(1, 2); g1.addEdge(1, 3); g1.addEdge(2, 3); g1.addEdge(3, 4); System.out.println("Coloring of graph 1"); g1.greedyColoring();
System.out.println(); Graph g2 = new Graph(5); g2.addEdge(0, 1); g2.addEdge(0, 2); g2.addEdge(1, 2); g2.addEdge(1, 4); g2.addEdge(2, 4); g2.addEdge(4, 3); System.out.println("Coloring of graph 2 "); g2.greedyColoring(); } } Output:--Coloring of graph 1 Vertex 0 ---> Color 0 Vertex 1 ---> Color 1 Vertex 2 ---> Color 2 Vertex 3 ---> Color 0 Vertex 4 ---> Color 1 Output:--Coloring of graph 2 Vertex 0 ---> Color 0 Vertex 1 ---> Color 1 Vertex 2 ---> Color 2 Vertex 3 ---> Color 0 Vertex 4 ---> Color 3 |
Translate psuedo code for computing chromatic number of a grapgh to java code 1 //graph G,...
Problem 12.29. A basic example of a simple graph with chromatic number n is the complete graph on n vertices, that is x(Kn) n. This implies that any graph with Kn as a subgraph must have chromatic number at least n. It's a common misconception to think that, conversely, graphs with high chromatic number must contain a large complete sub- graph. In this problem we exhibit a simple example countering this misconception, namely a graph with chromatic number four that...
8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can be done in O(n) time where n is the number of vertices in V. 8, (10 pts) Show that given a directed graph G = (V,E) already stored in adjacency matrix form, determining if there is a vertex with in-degree n - 1 and out-degree 0 can...
Help with Q3 please! 3 (9 pts) For the graph G (VE) in question 2 (above), construct the adjacency lists for G (using alphabetical ordering) and the corresponding reverse graph GR Adjacency list for G (alphabetical ordering): Adjacency list for G. V = {A, B, C, D, G, H, S) V - {A, B, C, D, G, H, S) E A = { EB = EC) - E[D] = {C,G) E[G] - [ ECH - E[S { EA = {...
B-1 Graph coloring Given an undirected graph G (V. E), a k-coloring of G is a function c : V → {0, 1, . . . ,k-1} such that c(u)≠c(v) for every edge (u, v) ∈ E. In other words, the numbers 0.1,... k-1 represent the k colors, and adjacent vertices different colors. must havec. Let d be the maximum degree of any vertex in a graph G. Prove that we can color G with d +1 colors.
(a) Given a graph G = (V, E) and a number k (1 ≤ k ≤ n), the CLIQUE problem asks us whether there is a set of k vertices in G that are all connected to one another. That is, each vertex in the ”clique” is connected to the other k − 1 vertices in the clique; this set of vertices is referred to as a ”k-clique.” Show that this problem is in class NP (verifiable in polynomial time)...
3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u) for some vertex v Consider the following algorithm that takes the adjacency list Alvi, v2, n] of a directed graph G as input and outputs an array containing all indegrees. An adjacency list Alvi, v.. /n] is an array indexed by the vertices in the graph. Each entry Alv, contains the list of neighbors of v) procedure Indegree(Alvi, v2,......
Problem 3 (15 points). Let G (V,E) be the following directed graph. a. 1. Draw the reverse graph G of G. 2. Run DFS on G to obtain a post number for each vertex. Assume that in the adjacency list representation of G, vertices are stored alphabetically, and in the list for each vertex, its adjacent vertices are also sorted alphabetically. In other words, the DFS algorithm needs to examine all vertices alphabetically, and when it traverses the adjacent vertices...
3. (4 points) Let Nm(G) be the the number of ways to properly color the vertices of a graph G with m colors. Pn and Cn are the path and circuit (or cycle) Show that on n vertices, respectively. Nm(P N(C= (m - 1) (-1)"-1 (m 1)"-2) 3. (4 points) Let Nm(G) be the the number of ways to properly color the vertices of a graph G with m colors. Pn and Cn are the path and circuit (or cycle)...
Problem 1: Dynamic Programming in DAG Let G(V,E), |V| = n, be a directed acyclic graph presented in adjacency list representation, where the vertices are labelled with numbers in the set {1, . . . , n}, and where (i, j) is and edge inplies i < j. Suppose also that each vertex has a positive value vi, 1 ≤ i ≤ n. Define the value of a path as the sum of the values of the vertices belonging to...
For a directed graph the in-degree of a vertex is the number of edges it has coming in to it, and the out- degree is the number of edges it has coming out. (a) Let G[i,j] be the adjacency matrix representation of a directed graph, write pseudocode (in the same detail as the text book) to compute the in-degree and out-degree of every vertex in the Page 1 of 2 CSC 375 Homework 3 Spring 2020 directed graph. Store results...