I am trying to figure out the sequence of nodes expansion of this graph when using applying DFS.
Should the sequence be A, B, C, D, E, G or A, B, D, E, G
Assuming ties are resolved in alphabetical order.
If you do DFS, choosing nodes in alphabetical order, the first path you find to G will be A, B, C, D, E, G.
Related
I have a pretty general question about algorithms:
Is there a shortest path algorithm in a graph (directed or undirected) with the following twist: each added node in the path is influenced by ALL of the other already existing nodes, and that "influence" (which is in other words - the weight of the edge between the two verteces, which is only created for that single purpose) is added to the path total weight?
Thanks in advance!
Edit: another attemp to explain: let's say I want to find the shortest path from A to Z, where in between there all kind of other nodes. The thing is, If im looking into the path that starts from A and then B, and I try to calculate the total distance from A to C, the distance is actually: distance(C) = weight(A, B) + weight(B,C) + weight(A, C), even though there isn't a direct edge between A and C, and its calculated just for that purpose.
Edit2: Another explanation: lets say we have: A, B1, B2, C1, C2, D. with verteces: (A, B1)=1, (A, B2)=2, (B1, C1)=1, (B2, C2)=2, (C1, D)=1, (C2, D)=2. (right to the equal sign is the edge weight). Shortest path from A to D is of course (A->B1->C1->D), but now lets add my bizzare demand: each node added to the path is adding his weight to all other nodes in the current path, so the total distance of the following is actually: 1+1+1+ w(A->C1) +w(A->D)+ w(B1->D). those weight can be known, but can't be used for the path itself (the A, B1, C1, D). one of those could effect the algorithms wanted result.
Your problem description (without further specifying weight(I, J)) is breaking one of the assumptions in the standard shortest path algorithms, namely that the shortest path from A to I might not be part of any shortest path A to Z via I.
A -> B(2), C(1)
B -> D(2)
C -> D(1)
D -> E(2)
with non-edge costs weight(I, J) := 0, except weight(C, E) := 100
The shortest path from A to D is A, C, D with cost 2=1+1, while the path A, B, D has cost 4=2+2. Even so the path A, C, D, E is more expensive than A, B, D, E because of weight(C, E).
In this case you're left with computing all possible paths from the start to the end node in your network, since the latest most expensive seeming path might still be cheaper when reaching the end node because of some weight(X, <end>): for each node, compute the cost of all incoming paths (from the start node) until you've computed all paths, then select the cheapest one at the end node. This algorithm assumes weight(I,J) >= 0 or that the input is a DAG.
I'm attempting to make certain that I understand a particular concept from a current homework assignment. In particular, we are given the Relation R = {A, B, C, D, E, F, G, H, I, J, K, L, M, N}, and the set F of functional dependencies for R.
{
A->B
AFE->GHI
ACEF->JKMEB
BD->LNB
ACFEBD->GIK
}
What I am attempting to list is five non-trivial elements of F^+. From my understanding, a dependency a->B is trivial if B is a subset of a. So, from that, I have determined the following five non-trivial elements.
A->G
A->H
B->J
B->K
D->L
Is this the correct way to be looking at this problem? Or do I misunderstand?
I want to find the weight of the edges of a graph by using the output of the Prim's algorithm.
Note: In a graph has n edges, each edge is different and between 1-n.
For example:
Vertices = {A, B, C, D, E}
Edges = {B-D, D-E, E-A, C-B, A-D, D-C, A-C}
Extract_Min() Order = B D C A E
By using the information above, I want to find the weight of each edge. Do you have any ideas?
Thanks in advance.
Edit: The solution does not have to be unique.
By your example:
Vertices = {A, B, C, D, E}
Edges = {B-D, D-E, E-A, C-B, A-D, D-C, A-C}
Extract_Min() Order = B D C A E
Look at the order given by Extract_Min().
The edge with weight 1 is surely B-D.
Assign weight 2 to some single edge from the set {B,D} to C.
Assign weight 3 to some single edge from the set {B,D,C} to A.
Assign weight 4 to some single edge from the set {B,D,C,A} to E.
Assign the remaining weights to the remaining edges in any order.
I'm looking for verification on the following in an effort of sorting out some basic properties of BFS & DFS.
Take the graph G=(V,E), V={a, b, c, d}, E={(a,b), (b,c), (c,d)}.
Suppose first G is a directed graph. Then, running BFS on
a as the source returns the subgraph V={a, b, c, d}, E={(a,b), (b,c), (c,d)},
b as the source returns the subgraph V={b, c, d}, E={(b,c), (c,d)},
c as the source returns the subgraph V={c, d}, E={(c,d)},
d as the source returns the subgraph V={d}, E={},
Running DFS on G can return, among others, one of the following 2 forests F. What
DFS returns depends on the ordering of nodes/edges on the representation of G:
i.) F={T_1}, T_1: V_{T_1}=V_G, E_{T_1}=E_G,
ii.) F={T_1, T_2, T_3}, V_{T_1}=V_{T_2}=V_{T_3}=V_G},
E_{T_1}={(a,b)}}, E_{T_2}={(b,c)}}, E_{T_3}={(c,d)}},
When G is undirected, Both BFS and DFS return a single tree, of which the edge- and vertex-sets are the same as those of G itself, and the tree roots/structures are potentially different depending the order of the elements of these sets and the nodes the algorithms started processing at.
I have a set of objects, and I need to produce a sorted list, but my comparison function is incomplete and leaves some room for "imagination" on the part of the sorting algorithm.
Specifically, given a collection of trees like the following:
A E
| |
B--C F
|
D
What I have on hand is the set of nodes {A, B, C, D, E, F}, in no particular order, and a function to test direct parent-child relationships:
parent(A, B)
parent(A, C)
parent(B, D)
parent(E, F)
I need to flatten this into a list where parents come before their children, but other than that, the order is unimportant. So any of these results would be acceptable:
A, B, C, D, E, F.
A, B, D, C, E, F.
E, F, A, B, C, D.
A, E, B, C, F, D.
The set will be small, a few dozen at most, so I'm not too concerned about performance. What I am concerned about is avoiding temporary data structures other than lists: due to limitations of the language I'm using, organizing these items into a temporary tree (for example) would be unpleasant.
You could use this simple algorithm:
while unsorted set is not empty {
elem = get element from unsorted set
while elem has parent and parent is in unsorted set {
elem = parent of elem
}
add elem to result list
remove elem from unsorted set
}
I believe you're looking for a topological sort algorithm.
Here's the relevant algorithm from the Wikipedia article:
L ← Empty list that will contain the sorted nodes
S ← Set of all nodes
function visit(node n)
if n has not been visited yet then
mark n as visited
for each node m with an edge from n to m do
visit(m)
add n to L
for each node n in S do
visit(n)