Docs

advertisement
A Quick Review of Graph Terminology

Graph– set of vertices and set of edges.
o More formally, a graph G = (V, E) is an ordered pair of finite sets V and E.
 The set V is the set of vertices. In the graph below, the vertices are
1,2,3,4,5,6
 The set E is the set of edges. In the graph below, the edges are 1-5,1-2,52,4-5,4-3,3-2,6-4

Edges can have an annotation describing the edge. If the annotation is a
value, weight, or cost, it is termed a weighted graph. In the graph below
the nodes could be homes and the edges could represent the cost of laying
cable between those homes.

The edges can have direction (The edge between 5 and 11 could mean "I
am to go from 5 to 11" or "5 supplies 11" or "5 likes 11".). A graph with
directed edges is termed a directedgraph or digraph. Otherwise, we call it
“undirected”.



Successor– the follows relationship in a directed graph (e.g., 8 follows 7.)
Degree– the number of edges incident to or touching a vertex. (The degree of 11 is 5)
o In-degree– the number of edges coming into a vertex (digraph only). (e.g., the
in-degree of 11 is 2.)
o Out-degree– the number of edges going out of a vertex (digraph only). (e.g., the
out-degree of 11 is 3.)
Predecessor– the precedes relationship in a directed graph. (e.g., 11 and 8 precede 9)
Two common methods of traversal are Breadth-First Search ( BFS ) or DepthFirst Search.
A Breadth-First Search. When a node is visited, all the siblings of the node
are visited before the children of the node are visited.
A Depth-First Search. When a node is visited, all the descendants of the
node are visited before the siblings of the node are visited.
Graph Traversal
To watch a short demonstration video click here.
Drawing Tools:
New Graph - Creates a random graph
Node tool - Used to insert new nodes. Click to place the node.
Hand tool - Used to select a node by clicking on the node or to move a node
click and drag the node.
Edge tool -Used to add an edge by clicking and dragging between two edges.
Eraser tool - Used to delete an edge or node. Click on the edge or node to
delete it.
Visit tool - Used to select the nodes in the order of traversal. Click on the
nodes in the order they should be traversed.
Edge Marking tools - Used to markForward, Back and Cross edges. Click on
the edge to mark it with the selected tool.
Standard Operations:
Find Path - Searches for a path from the highlighted start node to the node
containing the search value entered in the Search Valuetextfield.
Traverse - performs a traversal starting at the selected node.
Check - checks to see if the colored traversal is correct for the selected
options.
Clear Highlights - Resets the graph to an untraversed state.
Clear All - Deletes the entire graph
Additional Options:
Let Me Try - Allows you to select the nodes in the correct order of traversal
and label the edges.
Speed - Adjusts the Speed of the animation
Graph Options:
Directed - Specify a Directed or Undirected graph.
Weighted - Specify a Weighted or Unweighted graph.
Color Edges - Specify if the edges should be labeled as forward, back, and
cross edges.
Show Key - Controls the visibility of the color-key code used to color nodes
and edges
Search Options:
BFS - Specifies the traversal or search will be performed using a Breadth-First
Search
DFS - Specifies the traversal or search will be performed using a Depth-First
Search
Dijkstra - Specifies the traversal or search will be performed using Dijkstra's
Algorithm. The edges must be weighted to use this algorithm.
Submit:
Submit -Submits the graph to the instructor.
Print - Prints the graph.
Graph Traversal
To watch a short demonstration video click here.
Drawing Tools:
New Graph - Creates a random graph
Node tool - Used to insert new nodes. Click to place the node.
Hand tool - Used to select a node by clicking on the node or to move a node
click and drag the node.
Edge tool -Used to add an edge by clicking and dragging between two edges.
Eraser tool - Used to delete an edge or node. Click on the edge or node to
delete it.
Visit tool - Used to select the nodes in the order of traversal. Click on the
nodes in the order they should be traversed.
Edge Marking tools - Used to markForward, Back and Cross edges. Click on
the edge to mark it with the selected tool.
Standard Operations:
Find Path - Searches for a path from the highlighted start node to the node
containing the search value entered in the Search Valuetextfield.
Traverse - performs a traversal starting at the selected node.
Check - checks to see if the colored traversal is correct for the selected
options.
Clear Highlights - Resets the graph to an untraversed state.
Clear All - Deletes the entire graph
Additional Options:
Let Me Try - Allows you to select the nodes in the correct order of traversal
and label the edges.
Speed - Adjusts the Speed of the animation
Graph Options:
Directed - Specify a Directed or Undirected graph.
Weighted - Specify a Weighted or Unweighted graph.
Color Edges - Specify if the edges should be labeled as forward, back, and
cross edges.
Show Key - Controls the visibility of the color-key code used to color nodes
and edges
Search Options:
BFS - Specifies the traversal or search will be performed using a Breadth-First
Search
DFS - Specifies the traversal or search will be performed using a Depth-First
Search
Dijkstra - Specifies the traversal or search will be performed using Dijkstra's
Algorithm. The edges must be weighted to use this algorithm.
Submit:
Submit -Submits the graph to the instructor.
Print - Prints the graph.
BFS Traversal
Use the applet to the left to create a graph structure.
Then click Traverse to watch the applet traverse the graph structure.
#1 Why does the applet use a queue when performing a BFS?
Create a new graph structure.
Select Let Me Try and practice traversing the graph in the correct order.
Remember the idea of a queue.
To check if your traversal is correct click Check. Don't worry right now about
labeling the edges.
DFS Traversal
Use the applet to the left to create a graph structure.
Select DFS as the Search Method
Then click Traverse to watch the applet traverse the graph structure.
#2 Why does the applet use a stack when performing a DFS?
Create a new graph structure.
Select Let Me Try and practice traversing the graph in the correct order.
Remember the idea of a stack.
To check if your traversal is correct click Verify. Don't worry right now about
labeling the edges.
Connectedness
A common property used to describe graphs is connectedness.
A strongly connected graph is a graph such that there is a path from any
given node A to all other nodes.
A weakly connected graph is a graph such that if the edges were undirected
there would be a path from any given node A to all other nodes.
A graph that is not connected is a graph such that if the edges were
undirected, there would exist a node A such that there was not a path to
another node B
#3 Can you use graph traversal to determine the connectedness of a graph?
Does it matter if you used a DFS vs BFS? Would you expect this to be the
most effecient method to determine the connectedness of a graph?
Topological Ordering
Another common thing to do with graphs is to order the nodes
usingtopological ordering.
In order for a graph to have a topological order it must be adirected acyclic
graph ( DAG ) or in simpler terms a directed graph that has no cycles or
loops.
A topological order is used when precedence matters ie. A must be done
before B. A DAG is topologically ordered if the nodes are ordered such that
for each edge (i, j) i precedes j in the ordering.
#4 Can you use graph traversal to determine a topological ordering of a
graph? Does it matter if you used a DFS vs BFS? Would you expect this to
be the most effecient method to determine a topological ordering of a graph?
What is the big O runtime of this algorithm?
Shortest Path
Another common thing to do with graphs is to find the shortest path between
two nodes.
#5 Suppose you want to know the shortest path from node A to node E in an
unweighted graph. How could you use graph traversal to find the answer?
Would you want to use a DFS or a BFS?
Edge Labels
Edges can be label for a given traversal.
Tree Edge is an edge in the traversal path.
Back Edge is an edge that connects a vertex to an ancestor for a given
traversal
Forward Edge is an non tree edge that connects a vertex to a descendant.
Cross Edge is any other edge in a graph. It connects vertices in two different
trees or two vertices in the same tree neither of which is the ancestor of the
other.
In Let Me Try Mode traverse a graph and then try labeling the edges.
#6 Can a BFS Traversal ever have all four types of edges? If not, which one
will it be missing?
Detecting Cycles
By labeling the edges of a graph, one can easily determine if that graph is
cyclic.
A cyclic graph is a graph that has a loop or a cycle or in other words, a graph
such that there exists some node A such that there is a path from A to A.
#6 How can edge labels be used to detect cycles?
Download