SlideShare a Scribd company logo
CS 103 1
Graphs
• Definition of Graphs and Related
Concepts
• Representation of Graphs
• The Graph Class
• Graph Traversal
• Graph Applications
CS 103 2
Definition of Graphs
• A graph is a finite set of nodes with edges
between nodes
• Formally, a graph G is a structure (V,E)
consisting of
– a finite set V called the set of nodes, and
– a set E that is a subset of VxV. That is, E is a set of
pairs of the form (x,y) where x and y are nodes in
V
CS 103 3
Examples of Graphs
• V={0,1,2,3,4}
• E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
0
1
4
2
3
When (x,y) is an edge,
we say that x is adjacent to
y, and y is adjacent from x.
0 is adjacent to 1.
1 is not adjacent to 0.
2 is adjacent from 1.
CS 103 4
A “Real-life” Example of a Graph
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, of ages 12, 15, 12, 15, 13,
and 13, respectively.
• E ={(x,y) | if x is younger than y}
John Joe
Mary Helen
Tom Paul
CS 103 5
Intuition Behind Graphs
• The nodes represent entities (such as people, cities,
computers, words, etc.)
• Edges (x,y) represent relationships between entities x and
y, such as:
– “x loves y”
– “x hates y”
– “x is a friend of y” (note that this not necessarily reciprocal)
– “x considers y a friend”
– “x is a child of y”
– “x is a half-sibling of y”
– “x is a full-sibling of y”
• In those examples, each relationship is a different graph
CS 103 6
Graph Representation
• For graphs to be computationally useful,
they have to be conveniently represented in
programs
• There are two computer representations of
graphs:
– Adjacency matrix representation
– Adjacency lists representation
CS 103 7
Adjacency Matrix Representation
• In this representation, each graph of n nodes
is represented by an n x n matrix A, that is,
a two-dimensional array A
• The nodes are (re)-labeled 1,2,…,n
• A[i][j] = 1 if (i,j) is an edge
• A[i][j] = 0 if (i,j) is not an edge
CS 103 8
Example of Adjacency Matrix
0
1
4
2
3
0 1 0 1 0
0 0 1 0 0
0 0 1 0 0
1 0 0 0 0
0 0 0 4 0
A =
CS 103 9
Another Example of Adj. Matrix
• Re-label the nodes with numerical labels
John 2
Joe 3
Mary 0 Helen 1
Tom 4 Paul 5
0 0 0 0 0 0
0 0 0 0 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 1 0 0 0 0
1 1 0 0 0 0
A =
CS 103 10
Pros and Cons of Adjacency
Matrices
• Pros:
– Simple to implement
– Easy and fast to tell if a pair (i,j) is an edge:
simply check if A[i][j] is 1 or 0
• Cons:
– No matter how few edges the graph has, the
matrix takes O(n2) in memory
CS 103 11
Adjacency Lists Representation
• A graph of n nodes is represented by a one-
dimensional array L of linked lists, where
– L[i] is the linked list containing all the nodes
adjacent from node i.
– The nodes in the list L[i] are in no particular
order
CS 103 12
Example of Linked Representation
L[0]: empty
L[1]: empty
L[2]: 0, 1, 4, 5
L[3]: 0, 1, 4, 5
L[4]: 0, 1
L[5]: 0, 1
John 2
Joe
3
Mary 0 Helen 1
Tom 4 Paul 5
CS 103 13
Pros and Cons of Adjacency Lists
• Pros:
– Saves on space (memory): the representation
takes as many memory words as there are nodes
and edge.
• Cons:
– It can take up to O(n) time to determine if a pair
of nodes (i,j) is an edge: one would have to
search the linked list L[i], which takes time
proportional to the length of L[i].
CS 103 14
The Graph Class
class Graph {
public:
typedef int datatype;
typedef datatype * datatypeptr;
Graph( int n=0); // creates a graph of n nodes and no edges
bool isEdge( int i, int j);
void setEdge( int i, int j, datatype x);
int getNumberOfNodes(){return numberOfNodes;};
private:
datatypeptr *p; //a 2-D array, i.e., an adjacency matrix
int numberOfNodes;
};
CS 103 15
Graph Class Implementation
Graph::Graph( int n){
assert(n>=0);
numberOfNodes=n;
if (n==0) p=NULL;
else{
p = new datatypeptr[n];
for (int i=0;i<n;i++){
p[i] = new datatype[n];
for (int j=0;j<n;j++)
p[i][j]=0;
}
}
};
bool Graph::isEdge(int i, int j){
assert(i>=0 && j>=0);
return p[i][j] != 0;
};
void Graph:;setEdge(int i,
int j, datatype x){
assert(i>=0 && j>=0);
p[i][j]=x;
};
CS 103 16
Directed vs. Undirected Graphs
• If the directions of the edges matter, then we
show the edge directions, and the graph is
called a directed graph (or a digraph)
• The previous two examples are digraphs
• If the relationships represented by the edges
are symmetric (such as (x,y) is edge if and
only if x is a sibling of y), then we don’t
show the directions of the edges, and the
graph is called an undirected graph.
CS 103 17
Examples of Undirected Graphs
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, where the first 4 are siblings,
and the last two are siblings
• E ={(x,y) | x and y are siblings}
John Joe
Mary Helen
Tom Paul
if (x,y) is an edge:
we say that x is
adjacent to y, &
y adjacent to x.
We also say that
x and y are
neighbors
CS 103 18
Representations of Undirected
Graphs
• The same two representations for directed
graphs can be used for undirected graphs
• Adjacency matrix A:
– A[i][j]=1 if (i,j) is an edge; 0 otherwise
• Adjacency Lists:
– L[i] is the linked list containing all the
neighbors of i
CS 103 19
Example of Representations
Linked Lists:
L[0]: 1, 2, 3
L[1]: 0, 2, 3
L[2]: 0, 1, 3
L[3]: 0, 1, 2
L[4]: 5
L[5]: 4
John 2 Joe 3
Mary 0 Helen 1
Tom 4 Paul 5
0 1 1 1 0 0
1 0 1 1 0 0
1 1 0 1 0 0
1 1 1 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
A =
Adjacency Matrix:
CS 103 20
Definition of Some Graph Related
Concepts
• Let G be a directed graph
– The indegree of a node x in G is the number of
edges coming to x
– The outdegree of x is the number of edges
leaving x.
• Let G be an undirected graph
– The degree of a node x is the number of edges
that have x as one of their end nodes
– The neighbors of x are the nodes adjacent to x
CS 103 21
Things for You To Do
• Add a member function to the class graph,
called getIndegree( int x), which returns the
indegree of node x
• Add a member function to the class graph,
called getOutdegree( int x), which returns
the outdegree of node x
CS 103 22
Paths
• A path in a graph G is a sequence of nodes x1,
x2, …,xk, such that there is an edge from each
node the next one in the sequence
• For example, in the first example graph, the
sequence 3, 0, 1, 2 is a path, but the sequence
0, 3, 4 is not a path because (0,3) is not an edge
• In the “sibling-of” graph, the sequence John,
Mary, Joe, Helen is a path, but the
sequence Helen, Tom, Paul is not a path
CS 103 23
Graph Connectivity
• An undirected graph is said to be connected
if there is a path between every pair of
nodes. Otherwise, the graph is disconnected
• Informally, an undirected graph is
connected if it hangs in one piece
Disconnected Connected
CS 103 24
Connected Components
• If an undirected graph is not connected, then each
“piece” is called a connected component.
– A piece in itself is connected, but if you bring any other
node to it from the graph, it is no longer connected
• If the graph is connected, then the whole graph is
one single connected component
• Of Interest: Given any undirected graph G,
– Is G connected?
– If not, find its connected components.
CS 103 25
Graph Traversal Techniques
• The previous connectivity problem, as well
as many other graph problems, can be
solved using graph traversal techniques
• There are two standard graph traversal
techniques:
– Depth-First Search (DFS)
– Breadth-First Search (BFS)
CS 103 26
Graph Traversal (Contd.)
• In both DFS and BFS, the nodes of the
undirected graph are visited in a systematic
manner so that every node is visited exactly
one.
• Both BFS and DFS give rise to a tree:
– When a node x is visited, it is labeled as visited,
and it is added to the tree
– If the traversal got to node x from node y, y is
viewed as the parent of x, and x a child of y
CS 103 27
Depth-First Search
• DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
CS 103 28
Illustration of DFS
0 1
2
4
5
6
7
8
9
10
11
0
1
4
2
5
6
7
8
9
11
10
DFS Tree
Graph G
CS 103 29
Implementation of DFS
• Observations:
– the last node visited is the first node from
which to proceed.
– Also, the backtracking proceeds on the basis of
"last visited, first to backtrack too".
– This suggests that a stack is the proper data
structure to remember the current node and how
to backtrack.
CS 103 30
Illustrate DFS with a Stack
• We will redo the DFS on the previous
graph, but this time with stacks
• In Class
CS 103 31
DFS (Pseudo Code)
DFS(input: Graph G) {
Stack S; Integer x, t;
while (G has an unvisited node x){
visit(x); push(x,S);
while (S is not empty){
t := peek(S);
if (t has an unvisited neighbor y){
visit(y); push(y,S); }
else
pop(S);
}
}
}
CS 103 32
C++ Code for DFS
int * dfs(Graph G){ // returns a parent array representing the DFS tree
int n=G.getNumberOfNodes();
int * parent = new int[n];
Stack S(n); bool visited[n];
for ( int i=0; i<n; i++) visited[i]=false;
int x=0;// begin DFS from node 0
int numOfConnectedComponents=0;
while (x<n){ // begin a new DFS from x
numOfConnectedComponents++;
visited[x]=true; S.push(x); parent[x] = -1; // x is root
while(!S.isEmpty()) // traverse the current piece
// insert here the yellow box from the next slide
x= getNextUnvisited(visited,n,x);
}
cout<<“Graph has “<< numOfConnectedComponents<<
“ connected componentsn”;
return p;
}
CS 103 33
{
int t=S.peek( );
int y=getNextUnvisitedNeighbor(
t,G,visited,n);
if (y<n){
visited[y]=true;
S.push(y);
parent[y]=t;
}
else S.pop( );
}
//Put this before dfs(…). This returns the next unvisited node, or n otherwise
int getNextUnvisited(bool visited[],int n, int lastVisited){
int j=lastVisited+1;
while (visited[j] && j<n) j++;
return j;
}
// Put this before dfs(…)
// returns the leftmost unvisited
// neighbor of node t. If none
// remains, returns n.
int getNextUnvisitedNeighbor(int t,
graph G, bool visited[],int n){
for (int j=0;j<n;j++)
if (G.isEdge(t,j) && !visited[j])
return j;
// if no unvisited neighbors left:
return n;
}
CS 103 34
Breadth-First Search
• BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes from
this level form a new level that becomes the next
current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
CS 103 35
Illustration of BFS
0 1
2
4
5
6
7
8
9
10
11
0
1 4
2
5
6 7 8
9
11
10
BFS Tree Graph G
CS 103 36
Implementation of DFS
• Observations:
– the first node visited in each level is the first
node from which to proceed to visit new nodes.
• This suggests that a queue is the proper data
structure to remember the order of the steps.
CS 103 37
Illustrate BFS with a Queue
• We will redo the BFS on the previous
graph, but this time with queues
• In Class
CS 103 38
BFS (Pseudo Code)
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){
visit(y); Enqueue(y,Q);
}
}
}
}
CS 103 39
Things for you to Do
• Give a C++ implementation of BFS, using the
pseudo code as your guide
• Use BFS as a way to determine of the input graph
G is connected, and if not, to output the number of
connected components
• Modify BFS so that the level of each node in the
BFS tree is computed.
• Give another class for graph, this time using a
linked lists representation.

More Related Content

Similar to lecture11.ppt

10.graph
10.graph10.graph
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Pooja Bhojwani
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Saurabh Kaushik
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
LakshyaBaliyan2
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
PyData
 
Graphs
GraphsGraphs
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
Jonghoon Park
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
ae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.pptae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.ppt
Sushilkumar Jogdankar
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
Nbhm m. a. and m.sc. scholarship test 2006
Nbhm m. a. and m.sc. scholarship test 2006Nbhm m. a. and m.sc. scholarship test 2006
Nbhm m. a. and m.sc. scholarship test 2006
MD Kutubuddin Sardar
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
AbhinavAshish17
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
kailash shaw
 

Similar to lecture11.ppt (20)

10.graph
10.graph10.graph
10.graph
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam LermaGraph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
 
Graphs
GraphsGraphs
Graphs
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
ae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.pptae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.ppt
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Nbhm m. a. and m.sc. scholarship test 2006
Nbhm m. a. and m.sc. scholarship test 2006Nbhm m. a. and m.sc. scholarship test 2006
Nbhm m. a. and m.sc. scholarship test 2006
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
ALG5.1.ppt
ALG5.1.pptALG5.1.ppt
ALG5.1.ppt
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 

Recently uploaded

Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 

Recently uploaded (20)

Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 

lecture11.ppt

  • 1. CS 103 1 Graphs • Definition of Graphs and Related Concepts • Representation of Graphs • The Graph Class • Graph Traversal • Graph Applications
  • 2. CS 103 2 Definition of Graphs • A graph is a finite set of nodes with edges between nodes • Formally, a graph G is a structure (V,E) consisting of – a finite set V called the set of nodes, and – a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V
  • 3. CS 103 3 Examples of Graphs • V={0,1,2,3,4} • E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)} 0 1 4 2 3 When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1.
  • 4. CS 103 4 A “Real-life” Example of a Graph • V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12, 15, 13, and 13, respectively. • E ={(x,y) | if x is younger than y} John Joe Mary Helen Tom Paul
  • 5. CS 103 5 Intuition Behind Graphs • The nodes represent entities (such as people, cities, computers, words, etc.) • Edges (x,y) represent relationships between entities x and y, such as: – “x loves y” – “x hates y” – “x is a friend of y” (note that this not necessarily reciprocal) – “x considers y a friend” – “x is a child of y” – “x is a half-sibling of y” – “x is a full-sibling of y” • In those examples, each relationship is a different graph
  • 6. CS 103 6 Graph Representation • For graphs to be computationally useful, they have to be conveniently represented in programs • There are two computer representations of graphs: – Adjacency matrix representation – Adjacency lists representation
  • 7. CS 103 7 Adjacency Matrix Representation • In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a two-dimensional array A • The nodes are (re)-labeled 1,2,…,n • A[i][j] = 1 if (i,j) is an edge • A[i][j] = 0 if (i,j) is not an edge
  • 8. CS 103 8 Example of Adjacency Matrix 0 1 4 2 3 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 4 0 A =
  • 9. CS 103 9 Another Example of Adj. Matrix • Re-label the nodes with numerical labels John 2 Joe 3 Mary 0 Helen 1 Tom 4 Paul 5 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0 A =
  • 10. CS 103 10 Pros and Cons of Adjacency Matrices • Pros: – Simple to implement – Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0 • Cons: – No matter how few edges the graph has, the matrix takes O(n2) in memory
  • 11. CS 103 11 Adjacency Lists Representation • A graph of n nodes is represented by a one- dimensional array L of linked lists, where – L[i] is the linked list containing all the nodes adjacent from node i. – The nodes in the list L[i] are in no particular order
  • 12. CS 103 12 Example of Linked Representation L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 John 2 Joe 3 Mary 0 Helen 1 Tom 4 Paul 5
  • 13. CS 103 13 Pros and Cons of Adjacency Lists • Pros: – Saves on space (memory): the representation takes as many memory words as there are nodes and edge. • Cons: – It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i].
  • 14. CS 103 14 The Graph Class class Graph { public: typedef int datatype; typedef datatype * datatypeptr; Graph( int n=0); // creates a graph of n nodes and no edges bool isEdge( int i, int j); void setEdge( int i, int j, datatype x); int getNumberOfNodes(){return numberOfNodes;}; private: datatypeptr *p; //a 2-D array, i.e., an adjacency matrix int numberOfNodes; };
  • 15. CS 103 15 Graph Class Implementation Graph::Graph( int n){ assert(n>=0); numberOfNodes=n; if (n==0) p=NULL; else{ p = new datatypeptr[n]; for (int i=0;i<n;i++){ p[i] = new datatype[n]; for (int j=0;j<n;j++) p[i][j]=0; } } }; bool Graph::isEdge(int i, int j){ assert(i>=0 && j>=0); return p[i][j] != 0; }; void Graph:;setEdge(int i, int j, datatype x){ assert(i>=0 && j>=0); p[i][j]=x; };
  • 16. CS 103 16 Directed vs. Undirected Graphs • If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph) • The previous two examples are digraphs • If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph.
  • 17. CS 103 17 Examples of Undirected Graphs • V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are siblings, and the last two are siblings • E ={(x,y) | x and y are siblings} John Joe Mary Helen Tom Paul if (x,y) is an edge: we say that x is adjacent to y, & y adjacent to x. We also say that x and y are neighbors
  • 18. CS 103 18 Representations of Undirected Graphs • The same two representations for directed graphs can be used for undirected graphs • Adjacency matrix A: – A[i][j]=1 if (i,j) is an edge; 0 otherwise • Adjacency Lists: – L[i] is the linked list containing all the neighbors of i
  • 19. CS 103 19 Example of Representations Linked Lists: L[0]: 1, 2, 3 L[1]: 0, 2, 3 L[2]: 0, 1, 3 L[3]: 0, 1, 2 L[4]: 5 L[5]: 4 John 2 Joe 3 Mary 0 Helen 1 Tom 4 Paul 5 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 A = Adjacency Matrix:
  • 20. CS 103 20 Definition of Some Graph Related Concepts • Let G be a directed graph – The indegree of a node x in G is the number of edges coming to x – The outdegree of x is the number of edges leaving x. • Let G be an undirected graph – The degree of a node x is the number of edges that have x as one of their end nodes – The neighbors of x are the nodes adjacent to x
  • 21. CS 103 21 Things for You To Do • Add a member function to the class graph, called getIndegree( int x), which returns the indegree of node x • Add a member function to the class graph, called getOutdegree( int x), which returns the outdegree of node x
  • 22. CS 103 22 Paths • A path in a graph G is a sequence of nodes x1, x2, …,xk, such that there is an edge from each node the next one in the sequence • For example, in the first example graph, the sequence 3, 0, 1, 2 is a path, but the sequence 0, 3, 4 is not a path because (0,3) is not an edge • In the “sibling-of” graph, the sequence John, Mary, Joe, Helen is a path, but the sequence Helen, Tom, Paul is not a path
  • 23. CS 103 23 Graph Connectivity • An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected • Informally, an undirected graph is connected if it hangs in one piece Disconnected Connected
  • 24. CS 103 24 Connected Components • If an undirected graph is not connected, then each “piece” is called a connected component. – A piece in itself is connected, but if you bring any other node to it from the graph, it is no longer connected • If the graph is connected, then the whole graph is one single connected component • Of Interest: Given any undirected graph G, – Is G connected? – If not, find its connected components.
  • 25. CS 103 25 Graph Traversal Techniques • The previous connectivity problem, as well as many other graph problems, can be solved using graph traversal techniques • There are two standard graph traversal techniques: – Depth-First Search (DFS) – Breadth-First Search (BFS)
  • 26. CS 103 26 Graph Traversal (Contd.) • In both DFS and BFS, the nodes of the undirected graph are visited in a systematic manner so that every node is visited exactly one. • Both BFS and DFS give rise to a tree: – When a node x is visited, it is labeled as visited, and it is added to the tree – If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y
  • 27. CS 103 27 Depth-First Search • DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1.
  • 28. CS 103 28 Illustration of DFS 0 1 2 4 5 6 7 8 9 10 11 0 1 4 2 5 6 7 8 9 11 10 DFS Tree Graph G
  • 29. CS 103 29 Implementation of DFS • Observations: – the last node visited is the first node from which to proceed. – Also, the backtracking proceeds on the basis of "last visited, first to backtrack too". – This suggests that a stack is the proper data structure to remember the current node and how to backtrack.
  • 30. CS 103 30 Illustrate DFS with a Stack • We will redo the DFS on the previous graph, but this time with stacks • In Class
  • 31. CS 103 31 DFS (Pseudo Code) DFS(input: Graph G) { Stack S; Integer x, t; while (G has an unvisited node x){ visit(x); push(x,S); while (S is not empty){ t := peek(S); if (t has an unvisited neighbor y){ visit(y); push(y,S); } else pop(S); } } }
  • 32. CS 103 32 C++ Code for DFS int * dfs(Graph G){ // returns a parent array representing the DFS tree int n=G.getNumberOfNodes(); int * parent = new int[n]; Stack S(n); bool visited[n]; for ( int i=0; i<n; i++) visited[i]=false; int x=0;// begin DFS from node 0 int numOfConnectedComponents=0; while (x<n){ // begin a new DFS from x numOfConnectedComponents++; visited[x]=true; S.push(x); parent[x] = -1; // x is root while(!S.isEmpty()) // traverse the current piece // insert here the yellow box from the next slide x= getNextUnvisited(visited,n,x); } cout<<“Graph has “<< numOfConnectedComponents<< “ connected componentsn”; return p; }
  • 33. CS 103 33 { int t=S.peek( ); int y=getNextUnvisitedNeighbor( t,G,visited,n); if (y<n){ visited[y]=true; S.push(y); parent[y]=t; } else S.pop( ); } //Put this before dfs(…). This returns the next unvisited node, or n otherwise int getNextUnvisited(bool visited[],int n, int lastVisited){ int j=lastVisited+1; while (visited[j] && j<n) j++; return j; } // Put this before dfs(…) // returns the leftmost unvisited // neighbor of node t. If none // remains, returns n. int getNextUnvisitedNeighbor(int t, graph G, bool visited[],int n){ for (int j=0;j<n;j++) if (G.isEdge(t,j) && !visited[j]) return j; // if no unvisited neighbors left: return n; }
  • 34. CS 103 34 Breadth-First Search • BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.
  • 35. CS 103 35 Illustration of BFS 0 1 2 4 5 6 7 8 9 10 11 0 1 4 2 5 6 7 8 9 11 10 BFS Tree Graph G
  • 36. CS 103 36 Implementation of DFS • Observations: – the first node visited in each level is the first node from which to proceed to visit new nodes. • This suggests that a queue is the proper data structure to remember the order of the steps.
  • 37. CS 103 37 Illustrate BFS with a Queue • We will redo the BFS on the previous graph, but this time with queues • In Class
  • 38. CS 103 38 BFS (Pseudo Code) BFS(input: graph G) { Queue Q; Integer x, z, y; while (G has an unvisited node x) { visit(x); Enqueue(x,Q); while (Q is not empty){ z := Dequeue(Q); for all (unvisited neighbor y of z){ visit(y); Enqueue(y,Q); } } } }
  • 39. CS 103 39 Things for you to Do • Give a C++ implementation of BFS, using the pseudo code as your guide • Use BFS as a way to determine of the input graph G is connected, and if not, to output the number of connected components • Modify BFS so that the level of each node in the BFS tree is computed. • Give another class for graph, this time using a linked lists representation.