data structure computer graphs

22
Graphs

Upload: kumar

Post on 25-May-2015

1.309 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Data structure computer graphs

Graphs

Page 2: Data structure computer graphs

What is a graph?• A data structure that consists of a set of nodes

(vertices) and a set of edges that relate the nodes to each other

• The set of edges describes relationships among the vertices

Page 3: Data structure computer graphs

Formal definition of graphs

• A graph G is defined as follows:

G=(V,E)

V(G): a finite, nonempty set of vertices

E(G): a set of edges (pairs of vertices)

Page 4: Data structure computer graphs

Directed vs. undirected graphs

• When the edges in a graph have no direction, the graph is called undirected

Page 5: Data structure computer graphs

• When the edges in a graph have a direction, the graph is called directed (or digraph)

Directed vs. undirected graphs (cont.)

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)

Warning: if the graph is directed, the order of the vertices in each edge is

important !!

Page 6: Data structure computer graphs

• Trees are special cases of graphs!!

Trees vs graphs

Page 7: Data structure computer graphs

Graph terminology

• Adjacent nodes: two nodes are adjacent if they are connected by an edge

• Path: a sequence of vertices that connect two nodes in a graph

• Complete graph: a graph in which every vertex is directly connected to every other vertex

5 is adjacent to 77 is adjacent from 5

Page 8: Data structure computer graphs

• What is the number of edges in a complete directed graph with N vertices? 

N * (N-1)

Graph terminology (cont.)

Page 9: Data structure computer graphs

• What is the number of edges in a complete undirected graph with N vertices? 

N * (N-1) / 2

Graph terminology (cont.)

Page 10: Data structure computer graphs

Graph terminology (cont.)

• Multi-graph

• Cycle

• Loop

Page 11: Data structure computer graphs

• Weighted graph: a graph in which each edge carries a value

Graph terminology (cont.)

Page 12: Data structure computer graphs

Graph implementation

• Array-based implementation– A 1D array is used to represent the vertices– A 2D array (adjacency matrix) is used to

represent the edges

Page 13: Data structure computer graphs

Array-based implementation- Adjacency Matrix

Page 14: Data structure computer graphs

Graph implementation (cont.)

• Linked-list implementation– A 1D array is used to represent the vertices

– A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

Page 15: Data structure computer graphs

Linked-list implementation

Page 16: Data structure computer graphs

Adjacency Matrix for Un-Weighted Graphs

1. Adjacency Matrix

2. Matrix for edges

3. Matrix for 2 length paths

4. Matrix for 3 length paths

5. Matrix for n length paths

6. Path Matrix

7. Completely Connected Graph

Page 17: Data structure computer graphs

Graph searching

• Problem: find a path between two nodes of the graph (e.g., Austin and Washington)

• Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Page 18: Data structure computer graphs

Depth-First-Search (DFS)

• What is the idea behind DFS?– Travel as far as you can down a path – Back up as little as possible when you reach a

"dead end" (i.e., next vertex has been "marked" or there is no next vertex)

• DFS can be implemented efficiently using a stack

Page 19: Data structure computer graphs

DFS• Algorithm:

Starting Node is A

1. Initialize all nodes to ready state(Status=1)

2. Push starting nose A onto Stack and change its status to waiting state (Status=2).

3.Repeat steps 4 & 5 until STACK is empty.4. Pop Top node N. Process N and change its Status to the processed

state (Status=3).

5.Push onto Stack all the neighbors of N that are still in ready state and change their state as waiting state(State=2).

6. Exit.

Page 20: Data structure computer graphs

Breadth-First-Searching (BFS)

• What is the idea behind BFS?– Look at all possible paths at the same depth

before you go at a deeper level– Back up as far as possible when you reach a

"dead end" (i.e., next vertex has been "marked" or there is no next vertex)

Page 21: Data structure computer graphs

BFS• Algorithm:

Starting Node is A

1. Initialize all nodes to ready state(Status=1)

2. Push starting nose A onto Queue and change its status to waiting state (Status=2).

3.Repeat steps 4 & 5 until Queue is empty.4. Remove the front node N. Process N and change its Status to the

processed state (Status=3).

5.Add to the rear of Queue all the neighbors of N that are still in ready state and change their state to waiting (State=2).

6. Exit.

Page 22: Data structure computer graphs

• Examples of Both DFS & BFS.