Skip to content

Commit abc2d67

Browse files
authored
Merge pull request ephremdeme#1 from ephremdeme/master
Synced
2 parents 6c5b990 + 24b1d76 commit abc2d67

File tree

276 files changed

+14326
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

276 files changed

+14326
-127
lines changed

.all-contributorsrc

Lines changed: 381 additions & 25 deletions
Large diffs are not rendered by default.

Algorithms/Armstrong number.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//program to check if a number is armstrong number
2+
3+
#include <iostream>
4+
using namespace std;
5+
int main()
6+
{
7+
int n,r,sum=0,temp;
8+
cout<<"Enter the Number= ";
9+
cin>>n;
10+
temp=n;
11+
while(n>0)
12+
{
13+
r=n%10;
14+
sum=sum+(r*r*r);
15+
n=n/10;
16+
}
17+
if(temp==sum)
18+
cout<<"Armstrong Number."<<endl;
19+
else
20+
cout<<"Not Armstrong Number."<<endl;
21+
return 0;
22+
}

Algorithms/ArmstrongNumber.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def check_armstrong_num(n):
2+
sum_of_cubes = 0
3+
orig_n = n
4+
while n > 0:
5+
digit = n%10
6+
sum_of_cubes = sum_of_cubes + pow(digit, 3)
7+
n = n//10
8+
9+
if sum_of_cubes == orig_n:
10+
return "Is an Armstrong Number"
11+
else:
12+
return "Not an Armstrong Number"
13+
14+
number = int(input())
15+
result = check_armstrong_num(number)
16+
print(result)

Algorithms/BFS.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std ;
4+
vector <bool> vis ;
5+
vector < vector <int> > adj ;
6+
7+
void BFS(int s)
8+
{
9+
queue <int> q ;
10+
q.push(s) ;
11+
vis[s] = true ;
12+
while (!q.empty())
13+
{
14+
int f = q.front() ;
15+
q.pop() ;
16+
cout<<f<<" " ;
17+
for(auto u : adj[f])
18+
{
19+
if(vis[u]==false)
20+
{
21+
q.push(u) ;
22+
vis[u] = true ;
23+
}
24+
}
25+
}
26+
27+
}
28+
29+
int main()
30+
{ cout<<"Enter number of Nodes and Edges\n" ;
31+
int n , e ;
32+
cin>>n>>e ;
33+
vis.assign(n ,false) ;
34+
adj.assign(n , vector <int> ()) ;
35+
int x , y ;
36+
cout<<"Enter Edges\n" ;
37+
for (int i = 0; i < e; i++) {
38+
cin>>x>>y ;
39+
adj[x].push_back(y) ;
40+
adj[y].push_back(x) ;
41+
}
42+
cout<<"BFS path from source node 0 : " ;
43+
BFS(0) ;
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Checks if the entered string is a pallindrome or not.
3+
*/
4+
5+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
namespace DSA
10+
{
11+
class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
Dictionary<char, int> characterPairs = new Dictionary<char, int>();
16+
string input = Console.ReadLine();
17+
18+
foreach (char c in input)
19+
{
20+
if (characterPairs.ContainsKey(c))
21+
characterPairs[c]++;
22+
else
23+
characterPairs[c] = 1;
24+
}
25+
26+
int oddPairs = 0;
27+
foreach (KeyValuePair<char, int> kvp in characterPairs)
28+
{
29+
30+
if (characterPairs[kvp.Key] % 2 != 0)
31+
oddPairs++;
32+
}
33+
if (oddPairs > 1)
34+
Console.WriteLine("The string CANNOT be rearranged to a pallindrome");
35+
else
36+
Console.WriteLine("The string CAN be rearranged to a pallindrome");
37+
}
38+
}
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using DataStructures.Graphs;
5+
6+
namespace Algorithms.Graphs
7+
{
8+
public class DijkstraAllPairsShortestPaths<TGraph, TVertex>
9+
where TGraph : IGraph<TVertex>, IWeightedGraph<TVertex>
10+
where TVertex : IComparable<TVertex>
11+
{
12+
13+
Dictionary<TVertex, DijkstraShortestPaths<TGraph, TVertex>> _allPairsDjkstra;
14+
15+
public DijkstraAllPairsShortestPaths(TGraph Graph)
16+
{
17+
if (Graph == null)
18+
throw new ArgumentNullException();
19+
20+
// Initialize the all pair
21+
_allPairsDjkstra = new Dictionary<TVertex, DijkstraShortestPaths<TGraph, TVertex>>();
22+
23+
var vertices = Graph.Vertices;
24+
25+
foreach (var vertex in vertices)
26+
{
27+
var dijkstra = new DijkstraShortestPaths<TGraph, TVertex>(Graph, vertex);
28+
_allPairsDjkstra.Add(vertex, dijkstra);
29+
}
30+
}
31+
32+
33+
public bool HasPath(TVertex source, TVertex destination)
34+
{
35+
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
36+
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
37+
38+
return _allPairsDjkstra[source].HasPathTo(destination);
39+
}
40+
41+
42+
/// Returns the distance between source vertex and destination vertex.
43+
public long PathDistance(TVertex source, TVertex destination)
44+
{
45+
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
46+
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
47+
48+
return _allPairsDjkstra[source].DistanceTo(destination);
49+
}
50+
51+
/// Returns an enumerable collection of nodes that specify the shortest path from source vertex to destination vertex.
52+
public IEnumerable<TVertex> ShortestPath(TVertex source, TVertex destination)
53+
{
54+
if (!_allPairsDjkstra.ContainsKey(source) || !_allPairsDjkstra.ContainsKey(destination))
55+
throw new Exception("Either one of the vertices or both of them don't belong to Graph.");
56+
57+
return _allPairsDjkstra[source].ShortestPathTo(destination);
58+
}
59+
60+
}
61+
62+
}
63+
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using DataStructures.Graphs;
2+
using DataStructures.Heaps;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace Algorithms.Graphs
8+
{
9+
/// Computes Dijkstra's Shortest-Paths for Directed Weighted Graphs from a single-source to all destinations.
10+
public class DijkstraShortestPaths<TGraph, TVertex>
11+
where TGraph : IGraph<TVertex>, IWeightedGraph<TVertex>
12+
where TVertex : IComparable<TVertex>
13+
{
14+
private const long Infinity = long.MaxValue;
15+
private const int NilPredecessor = -1;
16+
17+
private long[] _distances;
18+
private int[] _predecessors;
19+
20+
private Dictionary<TVertex, int> _nodesToIndices;
21+
private Dictionary<int, TVertex> _indicesToNodes;
22+
23+
private MinPriorityQueue<TVertex, long> _minPriorityQueue;
24+
25+
private readonly TGraph _graph;
26+
private readonly TVertex _source;
27+
28+
public DijkstraShortestPaths(TGraph graph, TVertex source)
29+
{
30+
if (graph == null)
31+
throw new ArgumentNullException(nameof(graph));
32+
33+
if (source == null)
34+
throw new ArgumentNullException(nameof(source));
35+
36+
if (!graph.HasVertex(source))
37+
throw new ArgumentException("The source vertex doesn't belong to graph.");
38+
39+
if (graph.Edges.Any(edge => edge.Weight < 0))
40+
throw new ArgumentException("Negative edge weight detected.");
41+
42+
_graph = graph;
43+
_source = source;
44+
45+
_initialize();
46+
_dijkstra();
47+
}
48+
49+
/// The Dijkstra's algorithm.
50+
private void _dijkstra()
51+
{
52+
while (!_minPriorityQueue.IsEmpty)
53+
{
54+
var currentVertex = _minPriorityQueue.DequeueMin();
55+
var currentVertexIndex = _nodesToIndices[currentVertex];
56+
57+
var outgoingEdges = _graph.OutgoingEdges(currentVertex);
58+
foreach (var outgoingEdge in outgoingEdges)
59+
{
60+
var adjacentIndex = _nodesToIndices[outgoingEdge.Destination];
61+
var delta = _distances[currentVertexIndex] != Infinity ? _distances[currentVertexIndex] + outgoingEdge.Weight : Infinity;
62+
63+
if (delta < _distances[adjacentIndex])
64+
{
65+
_distances[adjacentIndex] = delta;
66+
_predecessors[adjacentIndex] = currentVertexIndex;
67+
68+
if (_minPriorityQueue.Contains(outgoingEdge.Destination))
69+
{
70+
_minPriorityQueue.UpdatePriority(outgoingEdge.Destination, delta);
71+
}
72+
else
73+
{
74+
_minPriorityQueue.Enqueue(outgoingEdge.Destination, delta);
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
private void _initialize()
82+
{
83+
var verticesCount = _graph.VerticesCount;
84+
85+
_distances = new long[verticesCount];
86+
_predecessors = new int[verticesCount];
87+
88+
_nodesToIndices = new Dictionary<TVertex, int>();
89+
_indicesToNodes = new Dictionary<int, TVertex>();
90+
_minPriorityQueue = new MinPriorityQueue<TVertex, long>((uint)verticesCount);
91+
92+
var vertices = _graph.Vertices.ToList();
93+
for (int i = 0; i < verticesCount; i++)
94+
{
95+
if (_source.Equals(vertices[i]))
96+
{
97+
_distances[i] = 0;
98+
_predecessors[i] = 0;
99+
}
100+
else
101+
{
102+
_distances[i] = Infinity;
103+
_predecessors[i] = NilPredecessor;
104+
}
105+
106+
_minPriorityQueue.Enqueue(vertices[i], _distances[i]);
107+
108+
_nodesToIndices.Add(vertices[i], i);
109+
_indicesToNodes.Add(i, vertices[i]);
110+
}
111+
}
112+
113+
/// Determines whether there is a path from the source vertex to this specified vertex.
114+
public bool HasPathTo(TVertex destination)
115+
{
116+
if (!_nodesToIndices.ContainsKey(destination))
117+
throw new ArgumentException("Graph doesn't have the specified vertex.");
118+
119+
var index = _nodesToIndices[destination];
120+
return _distances[index] != Infinity;
121+
}
122+
123+
/// Returns the distance between the source vertex and the specified vertex.
124+
public long DistanceTo(TVertex destination)
125+
{
126+
if (!_nodesToIndices.ContainsKey(destination))
127+
throw new ArgumentException("Graph doesn't have the specified vertex.");
128+
129+
var index = _nodesToIndices[destination];
130+
return _distances[index];
131+
}
132+
133+
/// Returns an enumerable collection of nodes that specify the shortest path from the source vertex to the destination vertex.
134+
public IEnumerable<TVertex> ShortestPathTo(TVertex destination)
135+
{
136+
if (!_nodesToIndices.ContainsKey(destination))
137+
throw new ArgumentException("Graph doesn't have the specified vertex.");
138+
139+
if (!HasPathTo(destination))
140+
{
141+
return null;
142+
}
143+
144+
var dstIndex = _nodesToIndices[destination];
145+
var stack = new Stack<TVertex>();
146+
147+
int index;
148+
for (index = dstIndex; _distances[index] != 0; index = _predecessors[index])
149+
{
150+
stack.Push(_indicesToNodes[index]);
151+
}
152+
stack.Push(_indicesToNodes[index]);
153+
154+
return stack;
155+
}
156+
}
157+
}
158+

0 commit comments

Comments
 (0)