Tuesday, March 8, 2022

Graphs Introduction






package pep.Day38;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Graphs_Introduction {
static class Edge {
int src;
int nbr;
int wt;

Edge(int src, int nbr, int wt) {
this.src = src;
this.nbr = nbr;
this.wt = wt;
}
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int vtces = Integer.parseInt(br.readLine());


ArrayList<Edge>[] graphs = new ArrayList[vtces];
for (ArrayList<Edge> graph : graphs) {
graph = new ArrayList<>();
}
int edges = Integer.parseInt(br.readLine());

for (int i = 0; i < edges; i++) {
String[] parts = br.readLine().split(" ");
int v1 = Integer.parseInt(parts[0]);
int v2 = Integer.parseInt(parts[1]);
int wt = Integer.parseInt(parts[2]);

graphs[v1].add(new Edge(v1, v2, wt));
graphs[v2].add(new Edge(v2, v1, wt));
}

int src = Integer.parseInt(br.readLine());
int dest = Integer.parseInt(br.readLine());
}
}



No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...