Skip to content
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

can i find the leader/source vertex of a directed acyclic graph ? #166

Open
AkashKumar7902 opened this issue Feb 9, 2024 · 5 comments · May be fixed by #169
Open

can i find the leader/source vertex of a directed acyclic graph ? #166

AkashKumar7902 opened this issue Feb 9, 2024 · 5 comments · May be fixed by #169
Labels
question Further information is requested

Comments

@AkashKumar7902
Copy link

No description provided.

@AkashKumar7902 AkashKumar7902 changed the title can i find the leader vertex of a directed acyclic graph ? can i find the leader/source vertex of a directed acyclic graph ? Feb 9, 2024
@AkashKumar7902
Copy link
Author

AkashKumar7902 commented Feb 9, 2024

A more wide application for implementing this would be to merge two graphs

g.addEdge(vertex v, <source vertex of DAG>

doing this will merge DAG into graph g on its vertex v.

UPD:
Found a solution for this,

first you must implement findsource function, I have provided the implementation just below this comment.
To merge two graphs, follow these steps:

  1. g1 = graph.Union(g1, g2)
  2. g1.addEdge(v, findsource(g2)), where v is a vertex of graph g1 from which we want to add an edge to complete the merge.

@AkashKumar7902
Copy link
Author

To all those who might be looking for a solution:

func FindSource[K comparable, T any](g graph.Graph[K, T]) ([]K, error) {
	if !g.Traits().IsDirected {
		return nil, fmt.Errorf("cannot find source of a non-DAG graph ")
	}

	predecessorMap, err := g.PredecessorMap()
	if err != nil {
		return nil, fmt.Errorf("failed to get predecessor map: %w", err)
	}

	var sources []K
	for vertex, predecessors := range predecessorMap {
		if len(predecessors) == 0 {
			sources = append(sources, vertex)
		}
	}
	return sources, nil
}

@dominikbraun dominikbraun added the question Further information is requested label Mar 6, 2024
@dominikbraun
Copy link
Owner

Hi, thanks for the late response! Using PredecessorMap and then backtrack to the root seems to be the correct approach. Is this issue then clarified for you? 😄

@AkashKumar7902
Copy link
Author

AkashKumar7902 commented Mar 6, 2024

@dominikbraun

Yes. I was wondering will there be a function getSource in the package itself. I can send a PR for it.

@dominikbraun
Copy link
Owner

@dominikbraun

Yes. I was wondering will there be a function getSource in the package itself. I can send a PR for it.

@AkashKumar7902 Yes, that sounds reasonable. Feel free to submit a PR!

@AkashKumar7902 AkashKumar7902 linked a pull request Mar 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants