-
-
Notifications
You must be signed in to change notification settings - Fork 342
Dinics algo #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Prathameshk2024
wants to merge
6
commits into
TheAlgorithms:master
Choose a base branch
from
Prathameshk2024:dinics_algo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,137
−0
Open
Dinics algo #261
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b30acf
bidirectional_bfs
Prathameshk2024 7a50116
feat-graph_colouring
Prathameshk2024 c798337
feat_dinics_algo
Prathameshk2024 60b7b13
Merge branch 'master' into dinics_algo
Prathameshk2024 2a2ffd8
Merge branch 'master' into dinics_algo
Prathameshk2024 7535bee
Merge branch 'master' into dinics_algo
Prathameshk2024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # ============================================================== | ||
| # Viterbi Algorithm — Hidden Markov Model (HMM) Decoding | ||
| # ============================================================== | ||
| # | ||
| # Description: | ||
| # The Viterbi algorithm finds the most probable sequence of | ||
| # hidden states (state path) that results in a given sequence of | ||
| # observed events in a Hidden Markov Model. | ||
| # | ||
| # Time Complexity: O(N * T) | ||
| # - N = number of hidden states | ||
| # - T = length of observation sequence | ||
| # | ||
| # Space Complexity: O(N * T) | ||
| # | ||
| # Input: | ||
| # states - vector of hidden states | ||
| # observations - vector of observed symbols | ||
| # start_prob - named vector of initial probabilities (state → prob) | ||
| # trans_prob - matrix of transition probabilities (from_state → to_state) | ||
| # emit_prob - matrix of emission probabilities (state → observation) | ||
| # | ||
| # Output: | ||
| # A list containing: | ||
| # best_path - most probable state sequence | ||
| # best_prob - probability of the best path | ||
| # | ||
| # Example usage provided at bottom of file. | ||
| # ============================================================== | ||
|
|
||
| viterbi <- function(states, observations, start_prob, trans_prob, emit_prob) { | ||
| N <- length(states) | ||
| T_len <- length(observations) | ||
|
|
||
| # Initialize matrices | ||
| V <- matrix(0, nrow = N, ncol = T_len) # probability table | ||
| path <- matrix(NA, nrow = N, ncol = T_len) # backpointer table | ||
|
|
||
| # Initialization step | ||
| for (i in 1:N) { | ||
| V[i, 1] <- start_prob[states[i]] * emit_prob[states[i], observations[1]] | ||
| path[i, 1] <- 0 | ||
| } | ||
|
|
||
| # Recursion step | ||
| for (t in 2:T_len) { | ||
| for (j in 1:N) { | ||
| probs <- V[, t - 1] * trans_prob[, states[j]] * emit_prob[states[j], observations[t]] | ||
| V[j, t] <- max(probs) | ||
| path[j, t] <- which.max(probs) | ||
| } | ||
| } | ||
|
|
||
| # Termination step | ||
| best_last_state <- which.max(V[, T_len]) | ||
| best_prob <- V[best_last_state, T_len] | ||
|
|
||
| # Backtrack the best path | ||
| best_path <- rep(NA, T_len) | ||
| best_path[T_len] <- best_last_state | ||
|
|
||
| for (t in (T_len - 1):1) { | ||
| best_path[t] <- path[best_path[t + 1], t + 1] | ||
| } | ||
|
|
||
| best_state_sequence <- states[best_path] | ||
|
|
||
| return(list( | ||
| best_path = best_state_sequence, | ||
| best_prob = best_prob | ||
| )) | ||
| } | ||
|
|
||
| # ============================================================== | ||
| # Example Usage and Test | ||
| # ============================================================== | ||
|
|
||
| cat("=== Viterbi Algorithm — Hidden Markov Model ===\n") | ||
|
|
||
| # Example: Weather HMM | ||
| # States: Rainy, Sunny | ||
| # Observations: walk, shop, clean | ||
| states <- c("Rainy", "Sunny") | ||
| observations <- c("walk", "shop", "clean") | ||
|
|
||
| # Start probabilities | ||
| start_prob <- c(Rainy = 0.6, Sunny = 0.4) | ||
|
|
||
| # Transition probabilities | ||
| trans_prob <- matrix(c( | ||
| 0.7, 0.3, # from Rainy to (Rainy, Sunny) | ||
| 0.4, 0.6 # from Sunny to (Rainy, Sunny) | ||
| ), nrow = 2, byrow = TRUE) | ||
| rownames(trans_prob) <- states | ||
| colnames(trans_prob) <- states | ||
|
|
||
| # Emission probabilities | ||
| emit_prob <- matrix(c( | ||
| 0.1, 0.4, 0.5, # Rainy emits (walk, shop, clean) | ||
| 0.6, 0.3, 0.1 # Sunny emits (walk, shop, clean) | ||
| ), nrow = 2, byrow = TRUE) | ||
| rownames(emit_prob) <- states | ||
| colnames(emit_prob) <- observations | ||
|
|
||
| # Observed sequence | ||
| obs_seq <- c("walk", "shop", "clean") | ||
|
|
||
| cat("Observation sequence:", paste(obs_seq, collapse = ", "), "\n") | ||
| result <- viterbi(states, obs_seq, start_prob, trans_prob, emit_prob) | ||
|
|
||
| cat("Most probable state sequence:\n") | ||
| cat(paste(result$best_path, collapse = " -> "), "\n") | ||
| cat("Probability of this sequence:", result$best_prob, "\n") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.