Sie sind auf Seite 1von 18

Introduction

Graphs are a generalization of trees. Like trees, graphs have nodes and edges. (The nodes are sometimes
called vertices,
and the edges are sometimes called arcs.)
However, graphs are more general than trees: In a graph, a node can have any number  of incoming edges (in a tree,
the root node cannot have any incoming edges, and the other nodes can only have one incoming edge).
Every tree is a graph, but not every graph is a tree.

There are two kinds of graphs, directed and undirected:

                     
Note that in a directed graph, the edges are arrows (are directed from one node to another) while in the
undirected graph the edges are plain lines (they have no direction). In a directed graph, you can only
go from node to node following the direction of the arrows, while in an undirected graph, you can go
either way along an edge. This means that in a directed graph it is possible to reach a "dead end“
Terminology

Here are two example graphs (one directed and one undirected) and the terminology to describe
them.                                      In the directed graph, there is an edge from node 2 to node 1; therefore:
•The two nodes are adjacent (they are neighbors).
•Node 2 is a predecessor of node 1.
•Node 1 is a successor of node 2.
•The source of the edge is node 2, and the target of the edge is node 1.
In the undirected graph, there is an edge between node 1 and node 3; therefore:
•Nodes 1 and 3 are adjacent (they are neighbors).
Now consider the following (directed) graph:

    

A path in a graph is a sequence of vertices connected by edges, with no repeated edges.


A simple path is a path with no repeated vertices.

In this graph, there is a path from node 2 to node 5:


2→1→5. There is a path from node 1 to node 2: 1→3→4→2. There is also a path
from node 1 back to itself: 1→3→4→2→1. The first two paths are acyclic paths:
no node is repeated; the last path is a cyclic path, because node 1 occurs twice.
Note that the layout of the graph is arbitrary –
The important thing is which nodes are connected to which other nodes. So, for example, the
following graph is the same as the one given above, it's just been drawn differently:

       

Also note that an edge can connect a node to itself; for example:

                 
A cycle is a path (with at least one edge) whose first and last vertices are
the same.

Two edges are parallel if they connect the same pair of vertices.


Some special kinds of graphs
 no cyclic paths (that contains no cycles) is called a DAG (a Directed Acyclic Graph).
•A directed graph that has

•An undirected graph that has an edge between every pair of nodes is called a complete graph. Here's an example:

      

A directed graph can also be a complete graph; in that case, there must be an edge from every node to every other node.
A graph that has values associated with its edges is called a weighted graph. The graph can be
either directed or undirected. The weights can represent things like:
•The cost of traversing the edge.
•The length of the edge.
•The time needed to traverse the edge.
Here's an example of a weighted, directed graph:

    
A graph is connected if, treating all edges as being undirected, there is a path from every node to
every other node. For example:

                      
1) Directed Graph
In a directed graph, each edge has a direction.
Example

In the above graph, we have seven vertices ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, and ‘g’, and eight edges ‘ab’, ‘cb’,
‘dc’, ‘ad’, ‘ec’, ‘fe’, ‘gf’, and ‘ga’. As it is a directed graph, each edge bears an arrow mark that shows
its direction. Note that in a directed graph, ‘ab’ is different from ‘ba’.
What is a Directed Acyclic Graph?

A directed acyclic graph is an acyclic graph that has a direction as well as a lack of cycles
.

The parts of the above graph are:


•Integer = the set for the the Vertices.
•Vertices set = {1,2,3,4,5,6,7}.
•Edge set = {(1,2), (1,3), (2,4), (2,5), (3,6), (4,7),
(5,7), (6,7)}.
In computer science, DAGs are also called wait-for-graphs. When a DAG is used to detect
a deadlock, it illustrates that a resources has to wait for another process to continue.
DFS
Traversal means visiting all the nodes of a graph. Depth first traversal or Depth first Search is a
recursive algorithm for searching all the vertices of a graph or tree data structure.
The data structure which is being used in DFS is stack. The process is similar to BFS
algorithm.

Algorithm
•Step 1: SET STATUS = 1 (ready state) for each node in G
•Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
•Step 3: Repeat Steps 4 and 5 until STACK is empty
•Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
•Step 5: Push on the stack all the neighbours of N that are in the ready state (whose
STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
•Step 6: EXIT
Example :
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to print all the nodes of the graph starting from node H, by using depth first search (DFS) algorithm .
Solution :
Push H onto the stack
STACK : H   
POP the top element of the stack i.e. H, print it and push all the neighbours of H onto the stack that
are is ready state.
Print H   
STACK : A   
Pop the top element of the stack i.e. A, print it and push all the neighbours of A onto the stack that
are in ready state.
Print A  
Stack : B, D  
Pop the top element of the stack i.e. D, print it and push all the neighbours of D onto the
stack that are in ready state.
1.Print D   
2.Stack : B, F   
Pop the top element of the stack i.e. F, print it and push all the neighbours of F onto the stack
that are in ready state.
3.Print F  
4.Stack : B  
Pop the top of the stack i.e. B and push all the neighbours
5.Print B   
6.Stack : C   
Pop the top of the stack i.e. C and push all the neighbours.
Print C   
Stack : E, G   
Pop the top of the stack i.e. G and push all its neighbours.
Print G  
Stack : E  
Pop the top of the stack i.e. E and push all its neighbours.
Print E  
Stack :  
Hence, the stack now becomes empty and all the nodes of the graph have been
traversed.
The printing sequence of the graph will be :
H → A → D → F → B → C → G → E  
BFS
Breadth First Search (BFS) Algorithm
Breadth first search is a graph traversal algorithm that starts traversing the graph from root
node and explores all the neighbouring nodes. Then, it selects the nearest node and explore
all the unexplored nodes. The algorithm follows the same process for each of the nearest
node until it finds the goal.
The algorithm of breadth first search is given below. The algorithm starts with examining the
node A and all of its neighbours. In the next step, the neighbours of the nearest node of A
are explored and process continues in the further steps. The algorithm explores all
neighbours of all the nodes and ensures that each node is visited exactly once and no node
is visited twice.
Algorithm

Step 1: SET STATUS = 1 (ready state)


for each node in G

Step 2: Enqueue the starting node A


and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
Step 5: Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
Example
Consider the graph G shown in the following image, calculate the minimum path p from node A to
node E. Given that each edge has a length of 1.

            
Solution:
Minimum Path P can be found by applying breadth first search
algorithm that will begin at node A and will end at E. the algorithm
uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds
all the nodes that are to be processed while QUEUE2 holds all the
nodes that are processed and deleted from QUEUE1.

Lets start examining the graph from Node A.


1. Add A to QUEUE1 and NULL to QUEUE2.
QUEUE1 = {A}  
QUEUE2 = {NULL}  
2. Delete the Node A from QUEUE1 and insert all its neighbours.
Insert Node A into QUEUE2
QUEUE1 = {B, D}  
QUEUE2 = {A}  
3. Delete the node B from QUEUE1 and insert all its neighbours.
Insert node B into QUEUE2.
QUEUE1 = {D, C, F}   
QUEUE2 = {A, B}  
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the only neighbour of it which has been inserted, we will not insert it again. Insert node D into QUEUE2.
QUEUE1 = {C, F}  
QUEUE2 = { A, B, D}  
5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to QUEUE2.
QUEUE1 = {F, E, G}  
QUEUE2 = {A, B, D, C}  
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours has already been added, we will not add them again. Add node F to QUEUE2.
QUEUE1 = {E, G}  
QUEUE2 = {A, B, D, C, F}  
7. Remove E from QUEUE1, all of E's neighbours has already been added to QUEUE1 therefore we will not add them again. All the nodes are visited and the target node i.e. E is encountered into QUEUE2.
QUEUE1 = {G}  
QUEUE2 = {A, B, D, C, F,  E}

Das könnte Ihnen auch gefallen