How to give same id to dictionary if they belong to same network
Question:
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}]
convert the above list to
output=[{'s':1,'t':2,'id':1},
{'s':2,'t':3,'id':1},
{'s':3,'t':1,'id':1},
{'s':4,'t':5,'id':2},
{'s':5,'t':6,'id':2},
{'s':6,'t':4,'id':2}]
info – we are giving the same id to the first 3 dictionaries in the list because they belong to the same network. (if you plot graph). same with the next 3 dictionaries
Answers:
You can use the depth-first search algorithm to calculate each node belonging to any component and this is by iterating the search algorithm on the unvisited nodes.
it’s be some like the code in this url:
https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/
this code it’s work:
let V;
let adjListArray=[];
let id = 1;
let ids = [];
function Graph(v)
{
V = v
for (let i = 0; i < V; i++) {
adjListArray.push([]);
}
}
// Adds an edge to an undirected graph
function addEdge(src,dest)
{
adjListArray[src].push(dest);
adjListArray[dest].push(src);
}
function DFSUtil(v,visited)
{
visited[v] = true;
ids[v] = id;
for (let x = 0; x < adjListArray[v].length; x++)
{
if (!visited[adjListArray[v][x]]){
DFSUtil(adjListArray[v][x], visited);
}
}
}
function connectedComponents()
{
let visited = new Array(V);
for(let i = 1; i < V; i++)
{
visited[i] = false;
}
for (let v = 1; v < V; ++v)
{
if (!visited[v])
{
DFSUtil(v, visited);
id++;
document.write("<br>");
}
}
}
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}];
Graph(my_dict.length + 1);
for(var i = 0 ; i < my_dict.length ; i++){
addEdge(my_dict[i].s,my_dict[i].t);
}
connectedComponents();
for(var i = 0 ; i < my_dict.length ; i++){
my_dict[i].id = ids[my_dict[i].s];
}
console.log(my_dict);
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}]
convert the above list to
output=[{'s':1,'t':2,'id':1},
{'s':2,'t':3,'id':1},
{'s':3,'t':1,'id':1},
{'s':4,'t':5,'id':2},
{'s':5,'t':6,'id':2},
{'s':6,'t':4,'id':2}]
info – we are giving the same id to the first 3 dictionaries in the list because they belong to the same network. (if you plot graph). same with the next 3 dictionaries
You can use the depth-first search algorithm to calculate each node belonging to any component and this is by iterating the search algorithm on the unvisited nodes.
it’s be some like the code in this url:
https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/
this code it’s work:
let V;
let adjListArray=[];
let id = 1;
let ids = [];
function Graph(v)
{
V = v
for (let i = 0; i < V; i++) {
adjListArray.push([]);
}
}
// Adds an edge to an undirected graph
function addEdge(src,dest)
{
adjListArray[src].push(dest);
adjListArray[dest].push(src);
}
function DFSUtil(v,visited)
{
visited[v] = true;
ids[v] = id;
for (let x = 0; x < adjListArray[v].length; x++)
{
if (!visited[adjListArray[v][x]]){
DFSUtil(adjListArray[v][x], visited);
}
}
}
function connectedComponents()
{
let visited = new Array(V);
for(let i = 1; i < V; i++)
{
visited[i] = false;
}
for (let v = 1; v < V; ++v)
{
if (!visited[v])
{
DFSUtil(v, visited);
id++;
document.write("<br>");
}
}
}
my_dict=[{'s':1,'t':2},
{'s':2,'t':3},
{'s':3,'t':1},
{'s':4,'t':5},
{'s':5,'t':6},
{'s':6,'t':4}];
Graph(my_dict.length + 1);
for(var i = 0 ; i < my_dict.length ; i++){
addEdge(my_dict[i].s,my_dict[i].t);
}
connectedComponents();
for(var i = 0 ; i < my_dict.length ; i++){
my_dict[i].id = ids[my_dict[i].s];
}
console.log(my_dict);