Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
greedy_algorithms Namespace Reference

for std::vector More...

Namespaces

namespace  dijkstra
 Functions for the Dijkstra algorithm implementation.
 
namespace  stable_matching
 Functions for the Gale-Shapley Algorithm.
 

Classes

class  DigitSeparation
 A class that provides methods to separate the digits of a large positive number. More...
 

Functions

bool can_jump (const std::vector< int > &nums)
 Checks whether the given element (default is 1) can jump to the last index.
 
template<typename T , std::size_t N, std::size_t M>
void findMinimumEdge (const T &infinity, const std::array< std::array< T, N >, M > &graph)
 Finds the minimum edge of the given graph.
 

Detailed Description

for std::vector

for uint32_t

for assert

For std::vector to store separated digits.

for assert for INT_MAX for IO operations

Greedy Algorithms

For reveresing the vector For assert() function to check for errors For abs() function For int64_t data type to handle large numbers For input/output operations

Greedy Algorithms

for std::u32int_t for std::vector for std::find

Greedy Algorithms

for assert for std::cout

Greedy Algorithms

for array for IO operations for numeric limits

Greedy Algorithms

Function Documentation

◆ can_jump()

bool greedy_algorithms::can_jump ( const std::vector< int > & nums)

Checks whether the given element (default is 1) can jump to the last index.

Parameters
numsarray of numbers containing the maximum jump (in steps) from that index
Returns
true if the index can be reached
false if the index can NOT be reached
42 {
43 size_t lastPos = nums.size() - 1;
44 for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
45 if (i + nums[i] >= lastPos) {
46 lastPos = i;
47 }
48 }
49 return lastPos == 0;
50}
T size(T... args)
Here is the call graph for this function:

◆ findMinimumEdge()

template<typename T , std::size_t N, std::size_t M>
void greedy_algorithms::findMinimumEdge ( const T & infinity,
const std::array< std::array< T, N >, M > & graph )

Finds the minimum edge of the given graph.

Parameters
infinityDefines the infinity of the graph
graphThe graph that will be used to find the edge
Returns
void
39 {
40 if (N != M) {
41 std::cout << "\nWrong input passed. Provided array has dimensions " << N
42 << "x" << M << ". Please provide a square matrix.\n";
43 return;
44 }
45 for (int i = 0; i < graph.size(); i++) {
46 int min = infinity;
47 int minIndex = 0;
48 for (int j = 0; j < graph.size(); j++) {
49 if (i != j && graph[i][j] != 0 && graph[i][j] < min) {
50 min = graph[i][j];
51 minIndex = j;
52 }
53 }
54 std::cout << i << " - " << minIndex << "\t" << graph[i][minIndex]
55 << "\n";
56 }
57}
Graph Algorithms.