Presentation is loading. Please wait.

Presentation is loading. Please wait.

– Graphs 1 Graph Categories Strong Components Example of Digraph

Similar presentations


Presentation on theme: "– Graphs 1 Graph Categories Strong Components Example of Digraph"— Presentation transcript:

1 – Graphs 1 Graph Categories Strong Components Example of Digraph
Main Index Contents – Graphs Graph Categories Example of Digraph Connectedness of Digraph Adjacency Matrix Adjacency Set vertexInfo Object Vertex Map and Vector vInfo VtxMap and Vinfo Example Breadth-First Search Algorithm (2 slides) Dfs() Strong Components Graph G and Its Transpose GT Shortest-Path Example (2 slides) Dijkstra Minimum-Path Algorithm (2 slides) Minimum Spanning Tree Example Minimum Spanning Tree: vertices A and B Completing the Minimum Spanning-Tree with Vertices C and D Summary Slides (4 slides)

2 Königsberg Bridge Problem
Land River Land A and D are two islands in the river; a, b, c, d, e, f, and g are bridges. Question: Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting area?

3 Euler’s Model

4 Graph definitions and notations
A graph G is a pair, G = (V, E), where V is a finite nonempty set of vertices of G and E  V x V., i.e., the elements of E are pair of elements of V and E is called edges. Let V(G) denote the set of vertices, and E(G) denote the set of edges of G. if elements of E(G) are ordered pairs, G is called directed graph or digraph; otherwise, G is called an undirected graph in which (u, v) and (v, u) represent the same edge. Let G be a graph, a graph H is called a subgraph of G if V(H)  V(G) and E(H)  E(G); i.e., every vertex of H is a vertex of G and every edge in H is an edge in G. A graph can be shown pictorially: a vertex is drawn as a circle, and an edge is drawn as a line. In a directed graph, the edges are drawn using arrows. Additional terms will be defined in the following slides.

5 5 Main Index Contents Graph Categories A graph is connected if each pair of vertices have a edge or path between them A complete graph is a connected graph in which each pair of vertices are linked by an edge

6 Graph with ordered edges are called directed graphs or digraphs
6 Main Index Contents Example of Digraph Graph with ordered edges are called directed graphs or digraphs

7 Connectedness of Digraph
7 Main Index Contents Connectedness of Digraph Strongly connected if there is a path from any vertex to any other vertex. Weakly connected if, for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vi,vj).

8 Representation of a graph: Adjacency Matrix
8 Main Index Contents Representation of a graph: Adjacency Matrix An m by m matrix (where m is the number of vertices in the graph), called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (vi,, vj). Its value is the weight of the edge, or –1 (or 0) if the edge does not exist.

9 Another way of Representing a graph: Adjacency list
9 Main Index Contents Another way of Representing a graph: Adjacency list

10 Definition of a node in a graph
template <class vType) class nodeType { public: vType vertex; nodeType<vType> *link; };

11 Operations on a Graph Common operations
Create a graph (demonstration based on adjacent list with a linked list) Clear the graph print a graph Determine if the graph is empty Traverse the graph Others (depending on applications)

12 Review: Linked list ADT
template <class Type> class nodeType { public: type info; nodetype<Type> *link; }; Template<class Type> class linkedListType { const linkedListtype<Type> &operator=(const linkedListtype<Type> &); void initializedList( ); bool isEmpty( ); void print( ); int length( ); void destroyList( );

13 Review: Linked list ADT continued
void retrieveFirst(Type &firstElement); void search(const Type &searchItem); // displys “Item is/is not found” void insertFirst(const Types &newItem); void insertLast(const Types &newItem); void deleteNode(const types &deleteItem); linkedListType( ); // initialize first = last = NULL linkedListType(const linkedListtype<type> &otherList); // copy constructor ~linkedListType( ); // delete all nodes in the list protedted: nodeType<Type> *first; nodeType<Type> *last; };

14 Review: Template // specifies an array data member with generic element // type elemType and its size with the following template template<class elemType, int size> class listType { public: private: int maxSize; int length; elemType listelem[size]; }; To create a list of 100 int elements: listType<int, 100> inList;

15 Graph ADT: Adjacent list approach – the interface
template<class vType, int size> class graphType { public: bool isEmpty( ); void createGraph( ); void clearGraph( ); // vertices deallocated void printGraph( ); graphtype( ); // default constructor; gsize = 0 and maxSize = size ~graphType( ); // storage deallocated protected: int maxSize; // max number of vertices int size; // current number of vertices linkedListGraph<vType> *graph; // array of pointers to create // the adjacency list };

16 Graph ADT: Adjacent list approach – the implementation
template<class vType, int size> bool graphType<vtype, size>::isEmpty( ) { return (gsize == 0); } To implement the createGraph( ) funciton: assuming the user is prompted to enter at run time 1) number of vertices, 2) each and every vertex and its adjacent vertices, e.g., 5 … -99 … -99 2 … Note that –99 is the sentinel value used to terminal the input loop.

17 Graph ADT: Adjacent list approach – the implementation
template<class vType, int size> bool graphType<vtype, size>::createGraph( ) { vtype vertex, adjacentVertex; if (gSize != 0) clearGraph( ); cout << “Enter # vertices followed by each individual vertices and “ << “its adjacent vertices ending with –99: ”; cin >> gSize; for (int i = 0; i < gSize; i++) { cin >> vertex; cin >> adjacentVertex; while (adjacentVertex != -99) { graph[vertex].insertLast(adjacentVertex); }

18 Graph ADT: Adjacent list approach – the implementation
template<class vType, int size> bool graphType<vtype, size>::clearGraph( ) { int i; for (i = 0; i < gSize; i++) graph[i].destroyList( ); gSize = 0; }

19 The following slides for reference only

20 20 Main Index Contents vertexInfo Object A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

21 Vertex Map and Vector vInfo
21 Main Index Contents Vertex Map and Vector vInfo To store the vertices in a graph, we provide a map<T,int> container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector

22 VtxMap and Vinfo Example

23 Breadth-First Search Algorithm

24 Breadth-First Search… (Cont.)

25 dfs()

26 Strong Components A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible.

27 Graph G and Its Transpose GT
The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.

28 Shortest-Path Example
The shortest-path algorithm includes a queue that indirectly stores the vertices, using the corresponding vInfo index. Each iterative step removes a vertex from the queue and searches its adjacency set to locate all of the unvisited neighbors and add them to the queue.

29 Shortest-Path Example
29 Main Index Contents Shortest-Path Example Example: Find the shortest path for the previous graph from F to C.

30 Dijkstra Minimum-Path Algorithm From A to D Example
30 Main Index Contents Dijkstra Minimum-Path Algorithm From A to D Example

31 Dijkstra Minimum-Path Algorithm From… (Cont…)

32 Minimum Spanning Tree Example
32 Main Index Contents Minimum Spanning Tree Example

33 Minimum Spanning Tree: Vertices A and B
33 Main Index Contents Minimum Spanning Tree: Vertices A and B

34 Completing the Minimum Spanning-Tree Algorithm with Vertices C and D

35 §- Undirected and Directed Graph (digraph)
35 Main Index Contents Summary Slide 1 §- Undirected and Directed Graph (digraph) - Both types of graphs can be either weighted or nonweighted.

36 §- Breadth-First, bfs()
36 Main Index Contents Summary Slide 2 §- Breadth-First, bfs() - locates all vertices reachable from a starting vertex - can be used to find the minimum distance from a starting vertex to an ending vertex in a graph.

37 §- Depth-First search, dfs()
37 Main Index Contents Summary Slide 3 §- Depth-First search, dfs() - produces a list of all graph vertices in the reverse order of their finishing times. - supported by a recursive depth-first visit function, dfsVisit() - an algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG) - forms the basis for an efficient algorithm that finds the strong components of a graph

38 §- Dijkstra's algorithm
38 Main Index Contents Summary Slide 4 §- Dijkstra's algorithm - if weights, uses a priority queue to determine a path from a starting to an ending vertex, of minimum weight - This idea can be extended to Prim's algorithm, which computes the minimum spanning tree in an undirected, connected graph.


Download ppt "– Graphs 1 Graph Categories Strong Components Example of Digraph"

Similar presentations


Ads by Google