Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Data Structures and Algorithms

Similar presentations


Presentation on theme: "CS212: Data Structures and Algorithms"— Presentation transcript:

1 CS212: Data Structures and Algorithms
Graphs

2 Outline Adjacency Matrix Adjacency list Linked list Depth First
Graphs (Basic Concepts) Implementations of Graphs Adjacency Matrix Adjacency list Linked list Graph Traversals Depth First Breadth First

3 Graphs A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E, Each edge is a pair (v, w), where v, w  V Edges are sometimes referred to as arcs If the pair (V, W) is ordered, then the graph is directed (Diagraphs) Vertex w is adjacent to v, if and only if (v,w)  E For undirected graph, edge (v, w) or (w, v) => w is adjacent to v and v is adjacent to w Sometimes an edge (v, w), has a third component, (v, w, c), known as either a weight or a cost

4 Graphs A path in a graph is a sequence of vertices w1, w2, w3, …… wn such that (wi, wi+1)  E for 1<=i<n The length is the number of edges, equals to n-1 A path from a vertex to itself with length 0 Path(v,v) is referred to as a loop

5 Undirected Graphs Examples
1 2 3 4 (Connected) Vertices {1, 2, 3, 4} Edges {(1,2), (1, 3), (2, 3), (2, 4), (3, 4)} 1 2 3 4 Path = {(1, 2),(2,4), (4, 3)}

6 Undirected Graphs Examples
1 2 3 4 Tree (Graphs without Cycle) 1 2 3 4 Disconnected

7 Graphs A Simple Path: When all vertices are distinct, except that the first and the last could be the same A cycle in a directed graph is a path of length at least 1, such that w1 = wn This cycle is simple if the path is simple A path u, v, u in an undirected graph should not be considered as a cycle, because u, v and v, u are the same edge In a directed graph, these are different A directed graph is acyclic if it has no cycles (D.A.G)

8 Graphs An undirected graph is connected, if there is a path from every vertex to every other vertex A directed graph with this property is called strongly connected If a directed graph is not strongly connected, but the underlying graph (without directions) is connected, then the graph is weakly connected A complete graph is a graph in which there is an edge between every pair of vertices

9 Directed Graphs Examples
Strongly Connected Directed Cycle Weakly Connected

10 Graphs Applications Airport System: Airport vertices and flights edges
Examples of a real life situation that can be modeled by graphs are Airport System: Airport vertices and flights edges Traffic flow: Each street intersection represents a vertex, and each street is an edge

11 Implementations of Graphs
Static Implementation Uses a two-dimensional array of integers Also called Adjacency Matrix Implementation Adjacency List Implementation Uses a one-dimensional array of pointers Linked List Implementation Uses a linked list of pointers Provides a complete dynamic implementation

12 1. Static Implementation of Graph
2 3 4 5 6 7 1 2 5 3 7 6 4

13 1. Static Implementation of Graph
class SGraph { private: int noOfVertices; bool **aMatrix; public: SGraph(int nv) { //creates adjacency matrix to represent graph noOfVertices = nv; //Dynamically creates rows of two dimensional array aMatrix = new bool*[noOfVertices]; //Dynamically creates columns of two dimensional array for(int i = 0; i<noOfVertices; i++) aMatrix[i] = new bool[noOfVertices]; } Class continues on next slide…

14 1. Static Implementation of Graph
void insertGraph() //inserts graph in adjacency matrix { char choice; for(int i = 0; i<noOfVertices; i++) for(int j = 0; j<noOfVertices; j++) { cout<<"is there an edge b/w vertex"<<i<<"and"<<j; cin>>choice; if(choice == 'y' || choice == 'Y') aMatrix[i][j] = 1; else aMatrix[i][j] = 0; } };

15 2. Adjacency List Implementation
For each vertex keep a list of all adjacent vertices 1 2 3 4 5 6 7 1 2 5 3 7 6 4

16 2. Adjacency List Implementation
class DGraph { private: int noOfVertices; SinglyLinkedList<int> *aList; public: //creates adjacency list to represent graph DGraph(int nv) noOfVertices = nv; aList = new SinglyLinkedList<int>[noOfVertices]; } Class continues on next slide…

17 2. Adjacency List Implementation
//inserts graph in adjacency list void insertGraph() { for(int i = 0; i<noOfVertices; i++) { int vertexNo; cout<<"Enter a number of vertex adjacent to vertex" <<i<<" Enter -1 to terminate"; cin>>vertexNo; while(vertexNo != -1) { aList[i].addToTail(vertexNo); } };

18 Implementations of Graphs
In most real life situations, the vertices have names, which are known at compile time, instead of numbers We have to provide a mapping of names to numbers using a hash table

19 Graph Traversals Traversing a graph consists of visiting each vertex only once We can traverse the graph by two ways: Depth first Breadth first

20 Depth First Graph Traversal
Depth-first search is a generalization of preorder traversal for trees It is basically a Last visited, First explored strategy Let V be the last vertex visited and N1, N2, …, Nk are adjacent to V A depth-first traversal will: visit N1, then proceed to traverse all the unvisited successors of N1, then proceed to traverse the remaining adjacent vertices of V in similar fashion

21 Depth First Graph Traversal
Declare an array visited of type bool and initialize it to false Each index of this array indicates weather a particular vertex is visited or not

22 Depth First Graph Traversal
//Algorithm for Depth First Traversal void depthFirstTraversal(int TotalVertices) { bool *visited = new int[TotalVertices]; for(int i = 0; i < TotalVertices; i++) visited[i] = false; while there is a vertex v which is not visited DFT(v, visited) } Algorithm Continues on next slide…

23 Depth First Graph Traversal
//Algorithm for DFT void DFT(Vertex v, bool visited[ ]) { Stack s; s.push(v); while(!s.isempty()) { v = s.pop(); if(visited[v] == false) { visited[v] = true; for each vertex u adjacent to v if(visited[u] == false) s.push(u); }

24 Breadth First Graph Traversal
It is basically a level order traversal Let V be the last node visited and N1, N2, …, Nk are adjacent to V A breadth-first traversal will visit N1, then N2, and so forth through Nk, then proceed to traverse all the unvisited vertices adjacent to N1, then traverse the unvisited vertices adjacent to N2,…Nk in similar fashion

25 Breadth First Graph Traversal
//Algorithm for Breadth First Traversal breadthFirstTraversal(Vertex v) { Add V to Queue & mark it While (Queue not Empty) { Remove Vertex from Queue & call it w for (each unvisited vertex u adjacent to w) mark u visited add u in Queue }

26 Breadth First Graph Traversal
C D B E F G I H

27 Breadth First Graph Traversal
C Queue B A C D B E F G I H

28 Breadth First Graph Traversal
Dqueue C B A C D B E F G I H

29 Breadth First Graph Traversal
Enqueu D D A C D B E F G I H

30 Breadth First Graph Traversal
Dqueue B D A C D B E F G I H

31 Breadth First Graph Traversal
Enqueue E, I, H E I H A C D B E F G I H

32 Breadth First Graph Traversal
Dqueue D E I H A C B D E I H F G

33 Breadth First Graph Traversal
Dqueue E I H A C B D E I H F G

34 Breadth First Graph Traversal
Enqueue F, G H F G A C D B E F G I H

35 Breadth First Graph Traversal
Dqueue I H F G A C D B E F G I H

36 Breadth First Graph Traversal
Dqueue I H F G A C D B E F G I H H already in queue i.e. visited

37 Breadth First Graph Traversal
Dqueue H F G A C B D E I H F G

38 Breadth First Graph Traversal
Dqueue F G A C B D E I H F G

39 Breadth First Graph Traversal
Dqueue G A C B D E I H F G


Download ppt "CS212: Data Structures and Algorithms"

Similar presentations


Ads by Google