program for degre of a graph
public class Degree
{
static class Graph
{
int v, e;
int[][] direction;
Graph(int v, int e) {
this.v =
v;
this.e =
e;
direction = new
int[v][];
for (int i = 0;
i < v; i++)
direction[i] = new int[v];
}
}
static Graph create(int v, int e)
{
Graph G = new Graph(v, e);
G.direction[0][1] = 1;
G.direction[0][2] = 1;
G.direction[0][3] = 1;
G.direction[1][0] = 1;
G.direction[1][3] = 1;
G.direction[2][0] = 1;
G.direction[2][3] = 1;
G.direction[3][0] = 1;
G.direction[3][1] = 1;
G.direction[3][2] = 1;
return G;
}
static int find(Graph G, int vertex)
{
int deg = 0;
for (int i = 0; i < G.v; i++)
{
if
(G.direction[vertex][i] == 1)
deg++;
}
return deg;
}
public static void main(String[] args)
{
int vers = 4;
int edgs = 5;
Graph G = create(vers, edgs);
int vert = 0;
int deg = find(G, vert);
System.out.println(deg);
}
}
OUTPUT
3
Note : the index of vertex starts from zero
.
Program for existence of path in graph
import java.io.*;
import java.util.*;
import java.util.LinkedList;
public class Graph
{
private int Vertex;
private LinkedList<Integer> adjacent[];
Graph(int vertex)
{
Vertex = vertex;
adjacent = new
LinkedList[vertex];
for (int i=0; i<vertex;
++i)
adjacent[i] =
new LinkedList();
}
void add(int vertex,int wt)
{
adjacent[vertex].add(wt);
}
Boolean isReach(int s, int d)
{
LinkedList<Integer>t;
boolean visit[] = new
boolean[Vertex];
LinkedList<Integer> qu = new
LinkedList<Integer>();
visit[s]=true;
qu.add(s);
Iterator<Integer> it;
while (qu.size()!=0)
{
s =
qu.poll();
int n;
it =
adjacent[s].listIterator();
while
(it.hasNext())
{
n = it.next();
if (n==d)
return true;
if (!visit[n])
{
visit[n] = true;
qu.add(n);
}
}
}
return false;
}
public static void main(String args[])
{
Graph gh = new Graph(4);
gh.add(0, 1);
gh.add(0, 2);
gh.add(1, 2);
gh.add(2, 0);
gh.add(2, 3);
gh.add(3, 3);
int start = 1;
int end = 3;
if (gh.isReach(start, end))
System.out.println("true");
else
System.out.println("false");;
}
}
OUTPUT
true
NOTE: Add edge as per your need .
Kindly Thumbs Up for the effort .
Thank you .
Task 3: Grid Graphs and Mazes Part A - Generating Grid Graphs In the lecture we...
Please answer question 2. Introduction to Trees
Thank you
1. Graphs (11 points) (1) (3 points) How many strongly connected components are in the three graphs below? List the vertices associated with each one. 00 (2) (4 points) For the graph G5: (a) (0.5 points) Specify the set of vertices V. (b) (0.5 points) Specify the set of edges E. (c) (1 point) Give the degree for each vertex. (d) (1 point) Give the adjacency matrix representation for this graph....
Goal Design and implement an algorithm to input the data representation of two graphs, determine if the graphs are complements of each other and, if so, outputs a message that the graphs are complements then outputs the degrees of the vertices of each graph, otherwise outputs a message to the contrary and immediately terminates. Details Input to your program consists of the representation of two graphs and will be in the following format: an integer m representing the number of...
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...
7. Graphs u, u2, u3, u4, u5, u6} and the (a) Consider the undirected graph G (V, E), with vertex set V set of edges E ((ul,u2), (u2,u3), (u3, u4), (u4, u5), (u5, u6). (u6, ul)} i. Draw a graphical representation of G. ii. Write the adjacency matrix of the graph G ii. Is the graph G isomorphic to any member of K, C, Wn or Q? Justify your answer. a. (1 Mark) (2 Marks) (2 Marks) b. Consider an...
hello there ,, can anyone give the solution of this Assuming a graph is represented as an adjacency matrix, write the pseudocode for an algorithm that can determine if any path exists between two vertices. The algorithm would accept as input: The nxn adjacency matrix for an undirected, unweighted graph A source vertex A destination vertex Returning as output: A boolean value indicating whether there is a path between the source and destination. You can use anything for variable/function names...
hello there ,, can anyone give the solution of this Assuming a graph is represented as an adjacency matrix, write the pseudocode for an algorithm that can determine if any path exists between two vertices. The algorithm would accept as input: The nxn adjacency matrix for an undirected, unweighted graph A source vertex A destination vertex Returning as output: A boolean value indicating whether there is a path between the source and destination. You can use anything for variable/function names...
Help
2 2. II. Use the previous graphs to create the following: 1. Adjacency matrix for G in 1. 2. Incidence matrix for G in 1. 3. Adjacency list for G in 3. 4. Adjacency matrix for I in 5. 5. What is the degree of vertex a in 2. 6. If is a subgraph from G in 2. II-(K, L) is a complete graph, K-(b,c,d) and K C V. Draw the graph
Solve all parts please
5. In the following problems, recall that the adjacency matrix (or incidence matrix) for a simple graph with n vertices is an n x n matrix with entries that are all 0 or 1. The entries on the diagonal are all 0, and the entry in the ih row and jth column is 1 if there is an edge between vertex i and vertex j and is 0 if there is not an edge between vertex...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...
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...