filename stringlengths 7 140 | content stringlengths 0 76.7M |
|---|---|
code/game_theory/src/game_of_nim_next_best_move/game_of_nim_next_best_move.py | def nim_sum(objectList, heaps):
nim = 0
# Calculate nim sum for all elements in the objectList
for i in objectList:
nim = nim ^ i
print("The nim sum is {}.".format(nim))
# Determine how many objects to remove from which heap
objects_to_remove = max(objectList) - nim
objects_to_rem... |
code/game_theory/src/game_of_nim_win_loss_prediction/game_of_nim_win_loss_prediction.py | # Code inspiration taken from Wikipedia's Python Implementation
import functools
MISERE = "misere"
NORMAL = "normal"
def nim(heaps, game_type):
"""
Computes next move for Nim, for both game types normal and misere.
Assumption : Both players involved would play without making mistakes.
if there is a ... |
code/game_theory/src/grundy_numbers_kayle/grundy_numbers_kayle.py | def SGsom(a, b):
"""Returns recurrence Calulation for a and b"""
x = 0
y = 2
while a != 0 or b != 0:
if a % y == b % y:
w = 0
else:
w = 1
x += w * (y / 2)
a -= a % y
b -= b % y
y *= 2
return x
def quickSort(array, left, right)... |
code/game_theory/src/minimax/minimax.py | """
Minimax game playing algorithm
Alex Day
Part of Cosmos by OpenGenus Foundation
"""
from abc import ABC, abstractmethod
import math
class Board(ABC):
@abstractmethod
def terminal():
"""
Determine if the current state is terminal
Returns
-------
bool: If the board i... |
code/git/undo-changes.md | please refer: https://iq.opengenus.org/p/5a525c5f-e1ac-418c-abac-ff118587dd83/
|
code/git/viewhist.md | please refer
https://iq.opengenus.org/p/911697a3-030c-481a-9793-39138000febd/
|
code/graph_algorithms/src/Number-of-Islands-using-DFS.cpp | // C++Program to count islands in boolean 2D matrix
#include <bits/stdc++.h>
using namespace std;
// A utility function to do DFS for a 2D
// boolean matrix. It only considers
// the 8 neighbours as adjacent vertices
void DFS(vector<vector<int>> &M, int i, int j, int ROW,
int COL)
{
//Base condition
//if i less th... |
code/graph_algorithms/src/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/README.MD | # Adjacency lists graph representation
See main.c for a small example.
|
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/lgraph_stack.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*/
#include "lgraph_stack.h"
/*
* Creates a stack of size n.
* @n The size of the stack.
* @stack A pointer to a stack struct.
* @return 0 on success, -1 on malloc failure.
*/
int
createLG_Stack(size_t n, LG_Stack* stack)
{
stack->n = n;
... |
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/lgraph_stack.h | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
* Stack for pushing nodes ids.
*/
#ifndef LGRAPH_STACK_H
#define LGRAPH_STACK_H
#include "lgraph_struct.h"
/*
* A common stack definition.
*/
typedef struct
{
size_t n;
Node_ID* nodes;
int head;
} LG_Stack;
/*
* Creates a stack of s... |
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/lgraph_struct.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*/
#include "lgraph_struct.h"
/* ---------------------------GETTERS-----------------------------------------*/
inline size_t gLG_NbNodes(L_Graph* graph) { return (graph->n); }
inline LG_Node* gLG_NodesArray(L_Graph* graph) { return (graph->nodes)... |
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/lgraph_struct.h | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
* Here we have the definition of a graph using adjacency-list representation.
* NB : LG stands for "list graph".
*/
#ifndef GRAPH_STRUCT_H
#define GRAPH_STRUCT_H
#include <stdlib.h>
typedef size_t Node_ID;
/*
* One directed edge.
*/
typedef... |
code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_c/main.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*/
#include <stdio.h>
#include "lgraph_struct.h"
int
main()
{
L_Graph g;
createLGraph(5, &g);
LGraph_addDEdge(&g, 1, 3);
LGraph_addUEdge(&g, 2, 1);
LGraph_addUEdge(&g, 3, 3);
LGraph_addDEdge(&g, 1, 0);
Node_ID i;
LG_Node* n;
... |
code/graph_algorithms/src/astar_algorithm/astar_algorithm.js | // Astar algorithm using a Binary Heap instead of a list
/*
Usage:
var astar = require('astar.js');
G = [
[0, ....., 0],
[...........],
[...........],
[0, ....., 0]
];
astar.search(G, start_point, target_point, {});
*/
(function(definition) {
/* global module, define */
if (typeof module === "object" && ... |
code/graph_algorithms/src/bellman_ford_algorithm/README.md | # Bellman Ford Algorithm
**Description :** Given a graph and a source vertex *src* in graph, find shortest paths from *src* to all vertices in the given graph. The graph may contain negative edges.
**Time Complexity :** O(VE) *(where V is number of vertices & E is number of edges)*
---
<p align="center">
A massive... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm.c | #include <stdio.h>
void relax(int u, int v, double w, double d[], int pi[]) {
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
pi[v] = u;
}
}
void initialize_single_source(double d[], int pi[], int s, int n) {
int i;
for (i = 1; i<= n; ++i) {
d[i] = 1000000000.0;
pi[i] = 0;
}
d[s] = 0.0;
}
int bellma... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm.cpp | #include <iostream>
#include <map>
#include <vector>
#include <utility>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <climits>
int nodes, edges;
// Path of Cosmos by OpenGenus Foundation
// function to calculate path from source to the given destination
void path_finding(int source, std::un... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm.js | /**
* Part of Cosmos by OpenGenus
* Javascript program for Bellman Ford algorithm.
* Given a graph and a source vertex, it finds the
* shortest path to all vertices from source vertex.
*/
class Graph {
constructor(noOfVertices) {
this.V = noOfVertices;
this.graph = [];
}
addEdge(u, v, w) {
this... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm.php | <?php
/*Part of Cosmos by OpenGenus Foundation*/
/*{source, destination, distance}*/
$edges = array(
array(0,1,5),
array(0,2,8),
array(0,3,-4),
array(1,0,-2),
array(2,1,-3),
array(2,3,9),
array(3,1,7),
array(3,4,2),
... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm.py | # part of cosmos by OpenGenus Foundation
from collections import defaultdict
# Part of Cosmos by OpenGenus Foundation
class Graph:
def __init__(self, vertices):
self.V = vertices # # of vertices
self.graph = []
# add edge to graph
def addEdge(self, u, v, w):
self.graph.appen... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm_adjacency_list.java | /**
* An implementation of the Bellman-Ford algorithm. The algorithm finds
* the shortest path between a starting node and all other nodes in the graph.
* The algorithm also detects negative cycles.
*
* @author William Fiset, [email protected]
**/
import java.util.*;
public class BellmanFordAdj... |
code/graph_algorithms/src/bellman_ford_algorithm/bellman_ford_algorithm_edge_list.java | /**
* An implementation of the Bellman-Ford algorithm. The algorithm finds
* the shortest path between a starting node and all other nodes in the graph.
* The algorithm also detects negative cycles.
*
* @author William Fiset, [email protected]
**/
public class BellmanFordEdgeList {
// A direc... |
code/graph_algorithms/src/biconnected_components/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/biconnected_components/biconnected_components.cpp | #include <iostream>
#include <list>
#include <stack>
#define NIL -1
using namespace std;
int totalCount = 0;
class Edge
{
public:
int u;
int v;
Edge(int u, int v);
};
Edge::Edge(int u, int v)
{
this->u = u;
this->v = v;
}
// A class that represents an directed graph
class Graph
{
int V; // N... |
code/graph_algorithms/src/biconnected_components/biconnected_components.java | import java.util.*;
public class Biconnectivity {
List<Integer>[] graph;
boolean[] visited;
Stack<Integer> stack;
int time;
int[] tin;
int[] lowlink;
List<List<Integer>> edgeBiconnectedComponents;
List<Integer> cutPoints;
List<String> bridges;
public List<List<Integer>> biconnectivity(List<Integer>[] graph... |
code/graph_algorithms/src/biconnected_components/biconnected_components.py | """
This program reads the structure of the undirected graph for stdin, you can
feed it with a file using input/output redirections.
The structure of the input is :
number_of_nodes number_of_edges
u1 v1
u2 v2
...
"""
def dfs(u, p):
global T
V[u] = 1
disc[u] = T
low[u] = T
T = T + 1
# Used... |
code/graph_algorithms/src/bipartite_check/bipartite_check.cpp | // C++ program to find out whether a
// given graph is Bipartite or not
#include <iostream>
#include <queue>
#define V 4
using namespace std;
// This function returns true if graph
// G[V][V] is Bipartite, else false
bool isBipartite(int G[][V], int src)
{
// Create a color array to store colors
// assig... |
code/graph_algorithms/src/bipartite_check/bipartite_check.java | // Java program to find out whether a given graph is Bipartite or not
import java.util.*;
import java.lang.*;
import java.io.*;
// Part of Cosmos by OpenGenus Foundation
class Bipartite
{
final static int V = 4; // No. of Vertices
// This function returns true if graph G[V][V] is Bipartite, else false
... |
code/graph_algorithms/src/bipartite_checking/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/bipartite_checking/bipartite_checking.cpp | #include <iostream>
#include <queue>
using namespace std;
int bipartite(int g[][10], int s, int n)
{
int *col = new int [n];
for (int i = 0; i < n; i++)
col[i] = -1;
queue<int> q;
col[s] = 1;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
i... |
code/graph_algorithms/src/bipartite_checking/bipartite_checking.java | import java.util.*;
import java.lang.*;
import java.io.*;
// Program to Check whether Graph is a Bipartite using BFS
public class BipartiteBfs
{
private int numberOfVertices;
private Queue<Integer> queue;
public static final int NO_COLOR = 0;
public static final int RED = 1;
public static final i... |
code/graph_algorithms/src/bipartite_checking/bipartite_checking2.cpp | #include <iostream>
#include <vector>
#include <algorithm>
/* Part of Cosmos by OpenGenus Foundation */
bool dfs(int v, std::vector<std::vector<int>> &g, std::vector<int> &dp)
{
for (int u : g[v])
{
if (!dp[u])
{
dp[u] = 3 - dp[v]; // 3 - 1 = 2; 3 - 2 = 1
dfs(u, g, dp);... |
code/graph_algorithms/src/bipartite_checking/bipartite_checking_adjacency_list.java | /**
* Part of Cosmos by OpenGenus Foundation.
*
* This file shows you how to determine if a graph is bipartite or not.
* This can be achieved in linear time by coloring the visited nodes.
*
* Time Complexity: O(V + E)
*
* @author William Fiset, [email protected]
**/
import java.util.*;
class... |
code/graph_algorithms/src/boruvka_minimum_spanning_tree/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/boruvka_minimum_spanning_tree/boruvka_minimum_spanning_tree.cpp | #include <iostream>
#include <vector>
const int MAXN = 10000;
const int inf = 0x3fffffff;
int nodeCount, edgesCount;
std::vector<std::pair<int, int>> graf[MAXN];
void readGraph()
{
std::cin >> nodeCount >> edgesCount;
for (int i = 0; i < edgesCount; i++)
{
int x, y, w;
std::cin >> x >> y ... |
code/graph_algorithms/src/breadth_first_search/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
# Breadth First Search Algorithm (BFS)
In this algorithm, we search for a target node among the neighbouring nodes at a level in the graph before moving to next level.
Thus, it is also called as level order traversal... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.c | #include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
int del(struct node** head,int* element);
int add(struct node** head,int element);
//main program
int main()
{
//input no of nodes: Num
int Num;
printf("Enter number of nodes\n");
scanf("%d",&Num);
//cre... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.cpp | #include <iostream>
#include <map>
#include <set>
#include <queue>
/* Part of Cosmos by OpenGenus Foundation */
void bfs(int start, // the node that we are currently at
std::map<int, std::set<int>>& adjList, // the adjacency list for each nod... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.java | // Java program to print BFS traversal from a given source vertex.
import java.io.*;
import java.util.*;
// Part of Cosmos by OpenGenus Foundation
// This class represents a directed Graph using adjacency list representation
class Bfs
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Ad... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.py | """ Part of Cosmos by OpenGenus Foundation"""
import collections
"""
Wrapper function for the print function.
Used as the default visitFunc for bfs
"""
def visitPrint(i):
print(i)
"""
A class representing a undirected graph of nodes.
An edge can be added between two nodes by calling addEdge
... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.rb | # This class represents a node in a graph.
# It has a NAME and an ID and each node knows which nodes it is connected to.
class GraphNode
require 'set'
@@id = 0
attr_reader :name, :id, :connections
def initialize(name)
@name = name
@id = (@@id += 1)
@connections = Set.new
end
def connect(n... |
code/graph_algorithms/src/breadth_first_search/breadth_first_search.swift | func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
var queue = Queue<Node>()
queue.enqueue(source)
var nodesExplored = [source.label]
source.visited = true
while let current = queue.dequeue() {
for edge in current.neighbors {
let neighborNode = edge.neighbor
if !neighborNode... |
code/graph_algorithms/src/bridge_tree/bridge_tree.cpp | #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <utility>
using namespace std;
typedef long long ll;
// Part of Cosmos by OpenGenus Foundation
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN], tree[MAXN]; // The bridge edge tree formed from the given graph.
int disc[MAXN], low[MAXN], ... |
code/graph_algorithms/src/bridges_in_graph/Count_bridges.py | # Python program to find bridges in a given undirected graph
#Complexity : O(V+E)
from collections import defaultdict
#This class represents an undirected graph using adjacency list representation
class Graph:
def __init__(self,vertices):
self.V= vertices #No. of vertices
self.graph = defau... |
code/graph_algorithms/src/bridges_in_graph/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/bridges_in_graph/bridges.cpp | #include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
// Part of Cosmos by OpenGenus Foundation
using namespace std;
#define WHITE 0
#define MAXV 15
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<int, int> mii;
vii adjList[MAXV];
vector<string> v_names;
mii d... |
code/graph_algorithms/src/bron_kerbosch_algorithm/bron_kerbosch_algorithm.java | import java.util.*;
public class BronKerbosh {
public static int BronKerbosch(long[] g, long cur, long allowed, long forbidden, int[] weights) {
if (allowed == 0 && forbidden == 0) {
int res = 0;
for (int u = Long.numberOfTrailingZeros(cur); u < g.length; u += Long.numberOfTrailingZeros(cur >> (u + 1)) + 1)
... |
code/graph_algorithms/src/centroid_decomposition/centroid_decomposition.cpp | #include <iostream>
#include <vector>
class Graph
{
public:
int ver_;
std::vector<std::vector<int>> adj_;
std::vector<std::vector<int>> centroidTree_;
std::vector<int> sizes_;
std::vector<bool> marked_;
Graph(int num) :
ver_(num),
adj_(num + 1),
centroidTree_(num + 1),
... |
code/graph_algorithms/src/centroid_decomposition/centroid_decomposition.java | import java.util.*;
public class CentroidDecomposition {
static void calcSizes(List<Integer>[] tree, int[] size, boolean[] deleted, int u, int p) {
size[u] = 1;
for (int v : tree[u]) {
if (v == p || deleted[v]) continue;
calcSizes(tree, size, deleted, v, u);
size[u] += size[v];
}
}
static int findT... |
code/graph_algorithms/src/channel_assignment/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/connected_components/connected_components.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*
* Connected Components on undirected graph based of depth-first search.
*
* You will need the data-structure located in
* https://github.com/OpenGenus/cosmos/tree/master/code/graph_algorithms/src/adjacency_lists_graph_representation/adjacenc... |
code/graph_algorithms/src/count_of_ways_n/count_of_ways_n.cpp | #include <iostream>
#include <vector>
const int MOD = 1000000007;
int size, n;
std::vector< std::vector<long long>> multiple (std::vector< std::vector<long long>> a,
std::vector< std::vector<long long>> b)
{
std::vector< std::vector<long long>> res(size);
for ... |
code/graph_algorithms/src/cut_vertices/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/cut_vertices/cut_vertices.cpp | #include <iostream>
#include <vector>
#include <climits>
using namespace std;
typedef long long ll;
// Part of Cosmos by OpenGenus Foundation
const int MAXN = 1e4 + 5;
vector<int> adj[MAXN];
bool vis[MAXN], AP[MAXN];
int n, m, currTime, disc[MAXN];
int low[MAXN]; // low[i] is the minimum of visited currTime of all ve... |
code/graph_algorithms/src/cycle_directed_graph/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/cycle_directed_graph/cycle_directed_graph.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*
* You will need the data-structure located in
* https://github.com/OpenGenus/cosmos/tree/master/code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_C
* to use this algorithm.
*/
#include "lgraph_struct.h"
#inc... |
code/graph_algorithms/src/cycle_directed_graph/cycle_directed_graph.cpp | #include <iostream>
#include <list>
#include <limits.h>
using namespace std;
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
bool isCyclicUtil(int v, bool visited[], bool *rs); // used by isCyclic()
public:
Graph(int V); // Constructor... |
code/graph_algorithms/src/cycle_directed_graph/cycle_directed_graph.py | from collections import defaultdict
class Graph:
def __init__(self, v):
self.v = v
self.adj = defaultdict(list)
def add_edge(self, u, v):
self.adj[u].append(v)
def find_parent(self, v, set_map):
return v if set_map[v] == -1 else self.find_parent(set_map[v], set_map)
... |
code/graph_algorithms/src/cycle_directed_graph/cycle_directed_graph_detection.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
*
* You will need the data-structure located in
* https://github.com/OpenGenus/cosmos/tree/master/code/graph_algorithms/src/adjacency_lists_graph_representation/adjacency_lists_in_C
* to use this algorithm.
*/
#include "lgraph_struct.h"
#inc... |
code/graph_algorithms/src/cycle_undirected_graph/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/cycle_undirected_graph/cycle_undirected_graph.cpp | /*
* Part of Cosmos by OpenGenus Foundation
*/
#include <iostream>
#include <list>
#include <limits.h>
using namespace std;
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
bool isCyclicUtil(int v, bool visited[], int parent);
public:
Gra... |
code/graph_algorithms/src/cycle_undirected_graph/cycle_undirected_graph.py | class graph:
def __init__(self):
self.neighbors = {}
def add_vertex(self, v):
if v not in self.neighbors:
self.neighbors[v] = []
def add_edge(self, u, v):
self.neighbors[u].append(v)
# if u == v, do not connect u to itself twice
if u != v:
se... |
code/graph_algorithms/src/cycle_undirected_graph/cycle_undirected_graph_check.java | // Java Program to detect cycle in an undirected graph.
// This program performs DFS traversal on the given graph
// represented by a adjacency matrix
// to check for cycles in the graph
import java.util.*;
import java.lang.*;
import java.io.*;
public class CheckCycle
{
private Stack <Integer> stack;
priv... |
code/graph_algorithms/src/cycle_undirected_graph/cycle_undirected_graph_union_find.cpp | /*this includes code for cheking the cycle is undirected graph using unionfind disjoint data structure
*
* the implementation is very fast
* as it implements unionfind disjoint structure using path-compression and rank heuristics
*/
#include <vector>
#include <cstddef>
int p[1000000]; //stores parent
int rnk[100... |
code/graph_algorithms/src/data_structures/README.md | # Graph data structure
## Adjacency list
Vertices are stored as records or objects, and every vertex stores a list of adjacent vertices. This data structure allows the storage of additional data on the vertices. Additional data can be stored if edges are also stored as objects, in which case each vertex stores its inci... |
code/graph_algorithms/src/data_structures/adjacency_list.cpp | /*
* Part of Cosmos by OpenGenus Foundation
*/
|
code/graph_algorithms/src/data_structures/adjacency_list.py | class graph:
def __init__(self):
self.graph={}
def graph_input(self,n):
for i in range(1,n+1):
self.graph.update({i:None})
element=[ ]
choi... |
code/graph_algorithms/src/data_structures/adjacency_matrix.cpp | /*
* Part of Cosmos by OpenGenus Foundation
*/
|
code/graph_algorithms/src/data_structures/adjacency_matrix.py | class graph:
adj=[]
def __init__(self,v,e):
self.v=v
self.e=e
graph.adj=[[0 for i in range(v)] for i in range(v)]
def adj_matrix(self,start,e):
graph.adj[start][e]=1
graph.adj[e][start]=1
def display_adj_matrix(self):
print("The adjacency... |
code/graph_algorithms/src/data_structures/adjacency_matrix_c/main.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
* How to use matrix-representation graphs.
*/
#include "mgraph_struct.h"
#include <stdio.h>
#define NB_NODES 5
int
main()
{
M_Graph g;
createMGraph(NB_NODES, &g);
int i, j;
for (i = 0; i < NB_NODES; ++i) {
for (j = 0; j < NB_NODES;... |
code/graph_algorithms/src/data_structures/adjacency_matrix_c/mgraph_struct.c | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
* Here we have the definition of a graph using adjacency-matrix representation.
* NB : MG stands for "matrix graph".
*/
#include "mgraph_struct.h"
/*
* Creates a new graph with matrix-list representation.
*
* @n Number of nodes.
* @graph Th... |
code/graph_algorithms/src/data_structures/adjacency_matrix_c/mgraph_struct.h | /*
* Part of Cosmos by OpenGenus Foundation.
* Author : ABDOUS Kamel
* Here we have the definition of a graph using adjacency-matrix representation.
* NB : MG stands for "matrix graph".
*/
#ifndef MGRAPH_STRUCT_H
#define MGRAPH_STRUCT_H
#include <stdlib.h>
/* Change this to change weighting type, if there is on... |
code/graph_algorithms/src/depth_first_search/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/depth_first_search/depth_first_search.c |
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
int push(struct node** head,int element);
int pop(struct node** head,int* element);
//main program
int main()
{
//input no of nodes: Num
int Num;
printf("Enter number of nodes\n");
scanf("%d",&Num);
//c... |
code/graph_algorithms/src/depth_first_search/depth_first_search.cpp | #include <iostream>
#include <map>
#include <set>
/* Part of Cosmos by OpenGenus Foundation */
void dfs(int current, // the node that we are currently at
std::map<int, std::set<int>>& adjList, // the adjacency list for each node
std::map<int, ... |
code/graph_algorithms/src/depth_first_search/depth_first_search.cs | // C# program to print DFS traversal from a given given graph
using System;
using System.Collections.Generic;
// This class represents a directed graph using adjacency list
// representation
public class Graph
{
private int V; // No. of vertices
// Array of lists for Adjacency List Representation
private ... |
code/graph_algorithms/src/depth_first_search/depth_first_search.go | package main
import "fmt"
// Part of Cosmos by OpenGenus Foundation
/**************************************
* Structures
**************************************/
// Define set structure (golang has no built-in set)
// Usage:
//
// s := make(set) // Create empty set
// s := set{7: true, 21: tru... |
code/graph_algorithms/src/depth_first_search/depth_first_search.java | // Java program to print DFS traversal from a given Graph
// Part of Cosmos by OpenGenus Foundation
import java.io.*;
import java.util.*;
// This class represents a directed Dfs using adjacency list representation
class Dfs {
private int V; // No. of vertices
// Array of lists for Adjacency List Represe... |
code/graph_algorithms/src/depth_first_search/depth_first_search.kt | // Part of Cosmos by OpenGenus Foundation
import java.util.LinkedList
class Dfs(private val vertices: Int) {
// Array of lists for Adjacency List Representation
private val adj: List<LinkedList<Int>> = (0 until vertices).map { LinkedList<Int>() }
// Function to add an edge into the Graph
fun addEdge... |
code/graph_algorithms/src/depth_first_search/depth_first_search.py | """ Part of Cosmos by OpenGenus Foundation"""
import collections
class Graph:
def __init__(self):
self.adjList = collections.defaultdict(set)
def addEdge(self, node1, node2):
self.adjList[node1].add(node2)
self.adjList[node2].add(node1)
def dfsHelper(current, graph, visited, visitF... |
code/graph_algorithms/src/depth_first_search/depth_first_search.rb | # This class represents a node in a graph.
# It has a NAME and an ID and each node knows which nodes it is connected to.
class GraphNode
require 'set'
@@id = 0
attr_reader :name, :id, :connections
def initialize(name)
@name = name
@id = (@@id += 1)
@connections = Set.new
end
def connect(n... |
code/graph_algorithms/src/dijkstra_shortest_path/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path.c | //for directed acyclig graphs!!
// Part of Cosmos by OpenGenus Foundation
#include<stdio.h>
#include<stdlib.h>
#define INF 999999
typedef struct node graph;
struct node{
int val;
graph *next;
int weight;
};
int src;
int wt_ans,val_ans;
int heap_count;
int flag[1000000];
struct node table[1000000];
struct node a[1000... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path.cpp | #include <iostream>
#include <vector>
#include <set>
#include <utility>
using namespace std;
// Part of Cosmos by OpenGenus Foundation
const int N = 1E4 + 1;
vector<pair<int, int>> graph[N];
set<pair<int, int>> pq;
int dist[N], vis[N];
int main()
{
int n, m; cin >> n >> m;
while (m--)
{
int u, v, ... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path.java | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import j... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path.js | /**
* Part of Cosmos by OpenGenus
* Javascript program for Dijkstra's single
* source shortest path algorithm. The program is
* for adjacency matrix representation of the graph
*/
class Graph {
constructor(graph) {
this.V = graph.length;
this.graph = graph;
}
printSolution(dist) {
console.log("... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path.py | # Python program for Dijkstra's single
# source shortest path algorithm. The program is
# for adjacency matrix representation of the graph
# Library for INT_MAX
import sys
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)] for row in range... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path_efficient.py | # Title: Dijkstra's Algorithm for finding single source shortest path from scratch
# Author: Shubham Malik
# References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
# Part of Cosmos by OpenGenus Foundation
import math
import sys
class PriorityQueue:
# Based on Min Heap
def __init__(self):
sel... |
code/graph_algorithms/src/dijkstra_shortest_path/dijkstra_shortest_path_gnu_fast.cpp | #include <climits>
#include <ext/pb_ds/priority_queue.hpp>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using namespace __gnu_pbds;
template <class X, class C = greater<X>>
using heap = __gnu_pbds::priority_queue<X, C>;
vector<heap<pair<int, int>>::point_iterator> pt;
vector<int> d;
vec... |
code/graph_algorithms/src/dinic_maximum_flow/dinic_maximum_flow.cpp | #include <vector>
#include <queue>
#include <cstring>
#include <iostream>
// Part of Cosmos by OpenGenus Foundation
struct edge
{
int to;
int cap;
int rev;
};
class dinic
{
private:
int INF;
std::vector<edge> G[100000];
int level[100000];
int iter[100000];
public:
dinic();
void add... |
code/graph_algorithms/src/eulerian_path/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/eulerian_path/eulerian_path.java | // A Java program to check if a given graph is Eulerian or not
import java.io.*;
import java.util.*;
import java.util.LinkedList;
class Graph
{
private int V; // vertices
// Adjacency List Representation
private LinkedList<Integer> adj[];
Graph(int v)
{
V = v;
adj = n... |
code/graph_algorithms/src/eulerian_path/eulerian_path.py | # Part of Cosmos by OpenGenus Foundation
# Python program to check if a given graph is Eulerian of not using eulerian path identification
# Complexity : O(V+E)
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V = vertices # No. of vertices
self.graph = default... |
code/graph_algorithms/src/fleury_algorithm_euler_path/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/floyd_warshall_algorithm/README.md | # cosmos
Your personal library of every algorithm and data structure code that you will ever encounter
Collaborative effort by [OpenGenus](https://github.com/opengenus) |
code/graph_algorithms/src/floyd_warshall_algorithm/floyd_warshall.go | package graph
import "math"
// WeightedGraph defining matrix to use 2d array easier
type WeightedGraph [][]float64
// Defining maximum value. If two vertices share this value, it means they are not connected
var Inf = math.Inf(1)
// FloydWarshall Returns all pair's shortest path using Floyd Warshall algorithm
func ... |
code/graph_algorithms/src/floyd_warshall_algorithm/floyd_warshall_algorithm.c | // C Program for Floyd Warshall Algorithm
// Part of Cosmos by OpenGenus Foundation
#include<stdio.h>
// Number of vertices in the graph
#define V 4
/* Define Infinite as a large enough value. This value will be used
for vertices not connected to each other */
#define INF 99999
// A function to print the soluti... |
code/graph_algorithms/src/floyd_warshall_algorithm/floyd_warshall_algorithm.cpp | /* Part of Cosmos by OpenGenus Foundation */
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 1000000000
void floydWarshall(int vertex, int adjacencyMatrix[][4])
{
// calculating all pair shortest path
for (int k = 0; k < vertex; k++)
for (int i = 0; i < vertex; i++)
... |
code/graph_algorithms/src/floyd_warshall_algorithm/floyd_warshall_algorithm.java | import java.util.*;
import java.lang.*;
import java.io.*;
// Part of Cosmos by OpenGenus Foundation
class FloydWarshall{
final static int INF = 99999, V = 4;
void floydWarshall(int graph[][])
{
int dist[][] = new int[V][V];
int i, j, k;
/* Initialize the solution matrix same as i... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.